Skip to content

Commit 06cf043

Browse files
authored
Make Ioctl::opcode a method instead of a constant (#1286)
1 parent 3121096 commit 06cf043

5 files changed

Lines changed: 40 additions & 16 deletions

File tree

src/fs/ioctl.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,10 @@ unsafe impl ioctl::Ioctl for Ficlone<'_> {
7979
type Output = ();
8080

8181
const IS_MUTATING: bool = false;
82-
const OPCODE: ioctl::Opcode = ioctl::Opcode::old(c::FICLONE as ioctl::RawOpcode);
82+
83+
fn opcode(&self) -> ioctl::Opcode {
84+
ioctl::Opcode::old(c::FICLONE as ioctl::RawOpcode)
85+
}
8386

8487
fn as_ptr(&mut self) -> *mut c::c_void {
8588
self.0.as_raw_fd() as *mut c::c_void

src/ioctl/mod.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ use bsd as platform;
8585
#[inline]
8686
pub unsafe fn ioctl<F: AsFd, I: Ioctl>(fd: F, mut ioctl: I) -> Result<I::Output> {
8787
let fd = fd.as_fd();
88-
let request = I::OPCODE.raw();
88+
let request = ioctl.opcode().raw();
8989
let arg = ioctl.as_ptr();
9090

9191
// SAFETY: The variant of `Ioctl` asserts that this is a valid IOCTL call
@@ -138,7 +138,7 @@ unsafe fn _ioctl_readonly(
138138
/// output as indicated by `output`.
139139
/// - That `output_from_ptr` can safely take the pointer from `as_ptr` and cast
140140
/// it to the correct type, *only* after the `ioctl` call.
141-
/// - That `OPCODE` uniquely identifies the `ioctl` call.
141+
/// - That the return value of `opcode` uniquely identifies the `ioctl` call.
142142
/// - That, for whatever platforms you are targeting, the `ioctl` call is safe
143143
/// to make.
144144
/// - If `IS_MUTATING` is false, that no userspace data will be modified by the
@@ -150,12 +150,6 @@ pub unsafe trait Ioctl {
150150
/// type.
151151
type Output;
152152

153-
/// The opcode used by this `ioctl` command.
154-
///
155-
/// There are different types of opcode depending on the operation. See
156-
/// documentation for the [`Opcode`] struct for more information.
157-
const OPCODE: Opcode;
158-
159153
/// Does the `ioctl` mutate any data in the userspace?
160154
///
161155
/// If the `ioctl` call does not mutate any data in the userspace, then
@@ -169,6 +163,12 @@ pub unsafe trait Ioctl {
169163
/// to `false` when it should be `true`.
170164
const IS_MUTATING: bool;
171165

166+
/// Get the opcode used by this `ioctl` command.
167+
///
168+
/// There are different types of opcode depending on the operation. See
169+
/// documentation for the [`Opcode`] struct for more information.
170+
fn opcode(&self) -> Opcode;
171+
172172
/// Get a pointer to the data to be passed to the `ioctl` command.
173173
///
174174
/// See trait-level documentation for more information.

src/ioctl/patterns.rs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ unsafe impl<Opcode: CompileTimeOpcode> Ioctl for NoArg<Opcode> {
3939
type Output = ();
4040

4141
const IS_MUTATING: bool = false;
42-
const OPCODE: self::Opcode = Opcode::OPCODE;
42+
43+
fn opcode(&self) -> self::Opcode {
44+
Opcode::OPCODE
45+
}
4346

4447
fn as_ptr(&mut self) -> *mut c::c_void {
4548
core::ptr::null_mut()
@@ -89,7 +92,10 @@ unsafe impl<Opcode: CompileTimeOpcode, Output> Ioctl for Getter<Opcode, Output>
8992
type Output = Output;
9093

9194
const IS_MUTATING: bool = true;
92-
const OPCODE: self::Opcode = Opcode::OPCODE;
95+
96+
fn opcode(&self) -> self::Opcode {
97+
Opcode::OPCODE
98+
}
9399

94100
fn as_ptr(&mut self) -> *mut c::c_void {
95101
self.output.as_mut_ptr().cast()
@@ -142,7 +148,10 @@ unsafe impl<Opcode: CompileTimeOpcode, Input> Ioctl for Setter<Opcode, Input> {
142148
type Output = ();
143149

144150
const IS_MUTATING: bool = false;
145-
const OPCODE: self::Opcode = Opcode::OPCODE;
151+
152+
fn opcode(&self) -> self::Opcode {
153+
Opcode::OPCODE
154+
}
146155

147156
fn as_ptr(&mut self) -> *mut c::c_void {
148157
addr_of_mut!(self.input).cast::<c::c_void>()
@@ -186,7 +195,10 @@ unsafe impl<'a, Opcode: CompileTimeOpcode, T> Ioctl for Updater<'a, Opcode, T> {
186195
type Output = ();
187196

188197
const IS_MUTATING: bool = true;
189-
const OPCODE: self::Opcode = Opcode::OPCODE;
198+
199+
fn opcode(&self) -> self::Opcode {
200+
Opcode::OPCODE
201+
}
190202

191203
fn as_ptr(&mut self) -> *mut c::c_void {
192204
(self.value as *mut T).cast()
@@ -244,7 +256,10 @@ unsafe impl<Opcode: CompileTimeOpcode> Ioctl for IntegerSetter<Opcode> {
244256
type Output = ();
245257

246258
const IS_MUTATING: bool = false;
247-
const OPCODE: self::Opcode = Opcode::OPCODE;
259+
260+
fn opcode(&self) -> self::Opcode {
261+
Opcode::OPCODE
262+
}
248263

249264
fn as_ptr(&mut self) -> *mut c::c_void {
250265
self.value

src/process/ioctl.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ unsafe impl ioctl::Ioctl for Tiocsctty {
3737
type Output = ();
3838

3939
const IS_MUTATING: bool = false;
40-
const OPCODE: ioctl::Opcode = ioctl::Opcode::old(c::TIOCSCTTY as ioctl::RawOpcode);
40+
41+
fn opcode(&self) -> ioctl::Opcode {
42+
ioctl::Opcode::old(c::TIOCSCTTY as ioctl::RawOpcode)
43+
}
4144

4245
fn as_ptr(&mut self) -> *mut c::c_void {
4346
(&0_u32) as *const u32 as *mut c::c_void

src/pty.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,10 @@ unsafe impl ioctl::Ioctl for Tiocgptpeer {
206206
type Output = OwnedFd;
207207

208208
const IS_MUTATING: bool = false;
209-
const OPCODE: ioctl::Opcode = ioctl::Opcode::old(c::TIOCGPTPEER as ioctl::RawOpcode);
209+
210+
fn opcode(&self) -> ioctl::Opcode {
211+
ioctl::Opcode::old(c::TIOCGPTPEER as ioctl::RawOpcode)
212+
}
210213

211214
fn as_ptr(&mut self) -> *mut c::c_void {
212215
self.0.bits() as *mut c::c_void

0 commit comments

Comments
 (0)