diff --git a/src/backend/v4l2/decoder/stateless.rs b/src/backend/v4l2/decoder/stateless.rs index 036fd4b6..6319a3cd 100644 --- a/src/backend/v4l2/decoder/stateless.rs +++ b/src/backend/v4l2/decoder/stateless.rs @@ -50,8 +50,8 @@ impl V4l2Picture { self.ref_pictures = None; self } - pub fn request(&self) -> &V4l2Request { - &self.request + pub fn request(&mut self) -> &mut V4l2Request { + &mut self.request } } diff --git a/src/decoder/stateless/h264/v4l2.rs b/src/decoder/stateless/h264/v4l2.rs index 7321b63d..8ac8d7c4 100644 --- a/src/decoder/stateless/h264/v4l2.rs +++ b/src/decoder/stateless/h264/v4l2.rs @@ -120,7 +120,7 @@ impl StatelessH264DecoderBackend for V4l2StatelessDecoderBackend { _: &[&DpbEntry], _: &[&DpbEntry], ) -> StatelessBackendResult<()> { - picture.borrow().request().write(slice.nalu.as_ref()); + picture.borrow_mut().request().write(slice.nalu.as_ref()); Ok(()) } @@ -128,7 +128,7 @@ impl StatelessH264DecoderBackend for V4l2StatelessDecoderBackend { let handle = Rc::new(RefCell::new(BackendHandle { picture: picture.clone(), })); - picture.borrow().request().submit(); + picture.borrow_mut().request().submit(); Ok(V4l2StatelessDecoderHandle { handle }) } } diff --git a/src/device/v4l2/stateless/request.rs b/src/device/v4l2/stateless/request.rs index 31c244a6..c831eff6 100644 --- a/src/device/v4l2/stateless/request.rs +++ b/src/device/v4l2/stateless/request.rs @@ -128,16 +128,19 @@ impl RequestHandle { }; self } - fn submit(self) -> Self { - match self { - Self::Init(handle) => Self::Pending(handle.submit()), + + // This method can modify in-place instead of returning a new value. This removes the need for + // a RefCell in V4l2Request. + fn submit(&mut self) { + match std::mem::take(self) { + Self::Init(handle) => *self = Self::Pending(handle.submit()), _ => panic!("ERROR"), } } - fn sync(self) -> Self { - match self { - Self::Pending(handle) => Self::Done(handle.sync()), - Self::Done(_) => self, + fn sync(&mut self) { + match std::mem::take(self) { + Self::Pending(handle) => *self = Self::Done(handle.sync()), + s @ Self::Done(_) => *self = s, _ => panic!("ERROR"), } } @@ -149,9 +152,7 @@ impl RequestHandle { } } -pub struct V4l2Request { - handle: RefCell, -} +pub struct V4l2Request(RequestHandle); impl V4l2Request { pub fn new( @@ -160,35 +161,31 @@ impl V4l2Request { handle: ioctl::Request, buffer: V4l2OutputBuffer, ) -> Self { - Self { - handle: RefCell::new(RequestHandle::new(device, timestamp, handle, buffer)), - } + Self(RequestHandle::new(device, timestamp, handle, buffer)) } pub fn timestamp(&self) -> u64 { - self.handle.borrow().timestamp() + self.0.timestamp() } - pub fn ioctl(&self, ctrl: C) -> &Self + pub fn ioctl(&mut self, ctrl: C) -> &mut Self where C: Into>, T: ExtControlTrait, { - self.handle.borrow_mut().ioctl(ctrl); + self.0.ioctl(ctrl); self } - pub fn write(&self, data: &[u8]) -> &Self { - self.handle.borrow_mut().write(data); + pub fn write(&mut self, data: &[u8]) -> &mut Self { + self.0.write(data); self } - pub fn submit(&self) -> &Self { - self.handle.replace(self.handle.take().submit()); - self + pub fn submit(&mut self) { + self.0.submit(); } - pub fn sync(&self) -> &Self { - self.handle.replace(self.handle.take().sync()); - self + pub fn sync(&mut self) { + self.0.sync(); } pub fn result(&self) -> V4l2Result { - self.handle.borrow().result() + self.0.result() } }