Skip to content

Commit

Permalink
decoder/stateless/v4l2: remove RefCell from V4l2Request
Browse files Browse the repository at this point in the history
This RefCell was there to provide interior mutability because some of
the methods did not take Self as mutable. With proper mutability
parameters, and a use of std::mem::take, it can be removed.
  • Loading branch information
Gnurou committed Jul 8, 2024
1 parent 1af2ce1 commit f51face
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 29 deletions.
4 changes: 2 additions & 2 deletions src/backend/v4l2/decoder/stateless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/decoder/stateless/h264/v4l2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,15 @@ impl StatelessH264DecoderBackend for V4l2StatelessDecoderBackend {
_: &[&DpbEntry<Self::Handle>],
_: &[&DpbEntry<Self::Handle>],
) -> StatelessBackendResult<()> {
picture.borrow().request().write(slice.nalu.as_ref());
picture.borrow_mut().request().write(slice.nalu.as_ref());
Ok(())
}

fn submit_picture(&mut self, picture: Self::Picture) -> StatelessBackendResult<Self::Handle> {
let handle = Rc::new(RefCell::new(BackendHandle {
picture: picture.clone(),
}));
picture.borrow().request().submit();
picture.borrow_mut().request().submit();
Ok(V4l2StatelessDecoderHandle { handle })
}
}
Expand Down
47 changes: 22 additions & 25 deletions src/device/v4l2/stateless/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
}
}
Expand All @@ -149,9 +152,7 @@ impl RequestHandle {
}
}

pub struct V4l2Request {
handle: RefCell<RequestHandle>,
}
pub struct V4l2Request(RequestHandle);

impl V4l2Request {
pub fn new(
Expand All @@ -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<C, T>(&self, ctrl: C) -> &Self
pub fn ioctl<C, T>(&mut self, ctrl: C) -> &mut Self
where
C: Into<SafeExtControl<T>>,
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()
}
}

Expand Down

0 comments on commit f51face

Please sign in to comment.