Skip to content

Commit 90b2fbd

Browse files
authored
Add ioctl Updater type (#925)
Many ioctls are defined with `_IOWR` and take input and return output through the same struct. We can handle this by taking an `&mut` pointer. I'm not sure about the name. `Updater` seems better than `GetterSetter`, but maybe something else would be clear.
1 parent e1d66b5 commit 90b2fbd

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

src/ioctl/patterns.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,49 @@ unsafe impl<Opcode: CompileTimeOpcode, Input> Ioctl for Setter<Opcode, Input> {
153153
}
154154
}
155155

156+
/// Implements an “updater” pattern for `ioctl`s.
157+
///
158+
/// The ioctl takes a reference to a struct that it reads its input from,
159+
/// then writes output to the same struct.
160+
pub struct Updater<'a, Opcode, Value> {
161+
/// Reference to input/output data.
162+
value: &'a mut Value,
163+
164+
/// The opcode.
165+
_opcode: PhantomData<Opcode>,
166+
}
167+
168+
impl<'a, Opcode: CompileTimeOpcode, Value> Updater<'a, Opcode, Value> {
169+
/// Create a new pointer updater-style `ioctl` object.
170+
///
171+
/// # Safety
172+
///
173+
/// - `Opcode` must provide a valid opcode.
174+
/// - For this opcode, `Value` must be the type that the kernel expects to
175+
/// get.
176+
#[inline]
177+
pub unsafe fn new(value: &'a mut Value) -> Self {
178+
Self {
179+
value,
180+
_opcode: PhantomData,
181+
}
182+
}
183+
}
184+
185+
unsafe impl<'a, Opcode: CompileTimeOpcode, T> Ioctl for Updater<'a, Opcode, T> {
186+
type Output = ();
187+
const IS_MUTATING: bool = true;
188+
const OPCODE: self::Opcode = Opcode::OPCODE;
189+
190+
fn as_ptr(&mut self) -> *mut c::c_void {
191+
(self.value as *mut T).cast()
192+
}
193+
194+
unsafe fn output_from_ptr(_output: IoctlOutput, _ptr: *mut c::c_void) -> Result<()> {
195+
Ok(())
196+
}
197+
}
198+
156199
/// Trait for something that provides an `ioctl` opcode at compile time.
157200
pub trait CompileTimeOpcode {
158201
/// The opcode.

0 commit comments

Comments
 (0)