Skip to content

Commit f51face

Browse files
committed
decoder/stateless/v4l2: remove RefCell from V4l2Request
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.
1 parent 1af2ce1 commit f51face

File tree

3 files changed

+26
-29
lines changed

3 files changed

+26
-29
lines changed

src/backend/v4l2/decoder/stateless.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ impl V4l2Picture {
5050
self.ref_pictures = None;
5151
self
5252
}
53-
pub fn request(&self) -> &V4l2Request {
54-
&self.request
53+
pub fn request(&mut self) -> &mut V4l2Request {
54+
&mut self.request
5555
}
5656
}
5757

src/decoder/stateless/h264/v4l2.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,15 @@ impl StatelessH264DecoderBackend for V4l2StatelessDecoderBackend {
120120
_: &[&DpbEntry<Self::Handle>],
121121
_: &[&DpbEntry<Self::Handle>],
122122
) -> StatelessBackendResult<()> {
123-
picture.borrow().request().write(slice.nalu.as_ref());
123+
picture.borrow_mut().request().write(slice.nalu.as_ref());
124124
Ok(())
125125
}
126126

127127
fn submit_picture(&mut self, picture: Self::Picture) -> StatelessBackendResult<Self::Handle> {
128128
let handle = Rc::new(RefCell::new(BackendHandle {
129129
picture: picture.clone(),
130130
}));
131-
picture.borrow().request().submit();
131+
picture.borrow_mut().request().submit();
132132
Ok(V4l2StatelessDecoderHandle { handle })
133133
}
134134
}

src/device/v4l2/stateless/request.rs

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -128,16 +128,19 @@ impl RequestHandle {
128128
};
129129
self
130130
}
131-
fn submit(self) -> Self {
132-
match self {
133-
Self::Init(handle) => Self::Pending(handle.submit()),
131+
132+
// This method can modify in-place instead of returning a new value. This removes the need for
133+
// a RefCell in V4l2Request.
134+
fn submit(&mut self) {
135+
match std::mem::take(self) {
136+
Self::Init(handle) => *self = Self::Pending(handle.submit()),
134137
_ => panic!("ERROR"),
135138
}
136139
}
137-
fn sync(self) -> Self {
138-
match self {
139-
Self::Pending(handle) => Self::Done(handle.sync()),
140-
Self::Done(_) => self,
140+
fn sync(&mut self) {
141+
match std::mem::take(self) {
142+
Self::Pending(handle) => *self = Self::Done(handle.sync()),
143+
s @ Self::Done(_) => *self = s,
141144
_ => panic!("ERROR"),
142145
}
143146
}
@@ -149,9 +152,7 @@ impl RequestHandle {
149152
}
150153
}
151154

152-
pub struct V4l2Request {
153-
handle: RefCell<RequestHandle>,
154-
}
155+
pub struct V4l2Request(RequestHandle);
155156

156157
impl V4l2Request {
157158
pub fn new(
@@ -160,35 +161,31 @@ impl V4l2Request {
160161
handle: ioctl::Request,
161162
buffer: V4l2OutputBuffer,
162163
) -> Self {
163-
Self {
164-
handle: RefCell::new(RequestHandle::new(device, timestamp, handle, buffer)),
165-
}
164+
Self(RequestHandle::new(device, timestamp, handle, buffer))
166165
}
167166
pub fn timestamp(&self) -> u64 {
168-
self.handle.borrow().timestamp()
167+
self.0.timestamp()
169168
}
170-
pub fn ioctl<C, T>(&self, ctrl: C) -> &Self
169+
pub fn ioctl<C, T>(&mut self, ctrl: C) -> &mut Self
171170
where
172171
C: Into<SafeExtControl<T>>,
173172
T: ExtControlTrait,
174173
{
175-
self.handle.borrow_mut().ioctl(ctrl);
174+
self.0.ioctl(ctrl);
176175
self
177176
}
178-
pub fn write(&self, data: &[u8]) -> &Self {
179-
self.handle.borrow_mut().write(data);
177+
pub fn write(&mut self, data: &[u8]) -> &mut Self {
178+
self.0.write(data);
180179
self
181180
}
182-
pub fn submit(&self) -> &Self {
183-
self.handle.replace(self.handle.take().submit());
184-
self
181+
pub fn submit(&mut self) {
182+
self.0.submit();
185183
}
186-
pub fn sync(&self) -> &Self {
187-
self.handle.replace(self.handle.take().sync());
188-
self
184+
pub fn sync(&mut self) {
185+
self.0.sync();
189186
}
190187
pub fn result(&self) -> V4l2Result {
191-
self.handle.borrow().result()
188+
self.0.result()
192189
}
193190
}
194191

0 commit comments

Comments
 (0)