#[non_exhaustive]pub struct StatusFlags(/* private fields */);Expand description
Implementations§
Source§impl StatusFlags
impl StatusFlags
Source§impl StatusFlags
impl StatusFlags
Sourcepub const fn bits(&self) -> u8
pub const fn bits(&self) -> u8
Get the underlying bits value.
The returned value is exactly the bits set in this flags value.
Examples found in repository?
27const DEFAULT_STATUS: StatusFlags =
28 StatusFlags::from_bits_truncate(StatusFlags::NOT_ERROR.bits() | StatusFlags::SELECTED.bits());
29
30fn ioctl_read_printer_status(file: &File) -> Result<u8> {
31 let getter = unsafe {
32 ioctl::Getter::<{ opcode::read::<u8>(GADGET_IOC_MAGIC, GADGET_GET_PRINTER_STATUS) }, u8>::new()
33 };
34 Ok(unsafe { ioctl::ioctl(file, getter) }?)
35}
36
37fn ioctl_write_printer_status(file: &File, status: u8) -> Result<()> {
38 let mut value = status;
39 unsafe {
40 ioctl::ioctl(
41 file,
42 ioctl::Updater::<{ opcode::read_write::<u8>(GADGET_IOC_MAGIC, GADGET_SET_PRINTER_STATUS) }, _>::new(
43 &mut value,
44 ),
45 )
46 }?;
47 Ok(())
48}
49
50fn create_printer_gadget() -> Result<RegGadget> {
51 usb_gadget::remove_all().expect("cannot remove all gadgets");
52
53 let udc = default_udc().expect("cannot get UDC");
54 let mut builder = Printer::builder();
55 builder.pnp_string = Some("Rust PNP".to_string());
56
57 let (_, func) = builder.build();
58 let reg = Gadget::new(
59 Class::INTERFACE_SPECIFIC,
60 Id::LINUX_FOUNDATION_COMPOSITE,
61 Strings::new("Clippy Manufacturer", "Rusty Printer", "RUST0123456"),
62 )
63 .with_config(Config::new("Config 1").with_function(func))
64 .bind(&udc)?;
65
66 Ok(reg)
67}
68
69fn read_printer_data(file: &mut File) -> Result<()> {
70 let mut buf = [0u8; BUF_SIZE];
71 let mut printed = 0;
72 println!("Will exit after printing {} pages...", PRINT_EXIT_COUNT);
73
74 loop {
75 let bytes_read = match file.read(&mut buf) {
76 Ok(bytes_read) if bytes_read > 0 => bytes_read,
77 _ => break,
78 };
79 io::stdout().write_all(&buf[..bytes_read])?;
80 io::stdout().flush()?;
81
82 // check if %%EOF is in the buffer
83 if buf.windows(5).any(|w| w == b"%%EOF") {
84 printed += 1;
85 if printed == PRINT_EXIT_COUNT {
86 println!("Printed {} pages, exiting.", PRINT_EXIT_COUNT);
87 break;
88 }
89 }
90 }
91
92 Ok(())
93}
94
95fn set_printer_status(file: &File, flags: StatusFlags, clear: bool) -> Result<StatusFlags> {
96 let mut status = get_printer_status(file)?;
97 if clear {
98 status.remove(flags);
99 } else {
100 status.insert(flags);
101 }
102 let bits = status.bits();
103 log::debug!("Setting printer status: {:08b}", bits);
104 ioctl_write_printer_status(file, bits)?;
105 Ok(StatusFlags::from_bits_truncate(bits))
106}Sourcepub const fn from_bits(bits: u8) -> Option<Self>
pub const fn from_bits(bits: u8) -> Option<Self>
Convert from a bits value.
This method will return None if any unknown bits are set.
Sourcepub const fn from_bits_truncate(bits: u8) -> Self
pub const fn from_bits_truncate(bits: u8) -> Self
Convert from a bits value, unsetting any unknown bits.
Examples found in repository?
27const DEFAULT_STATUS: StatusFlags =
28 StatusFlags::from_bits_truncate(StatusFlags::NOT_ERROR.bits() | StatusFlags::SELECTED.bits());
29
30fn ioctl_read_printer_status(file: &File) -> Result<u8> {
31 let getter = unsafe {
32 ioctl::Getter::<{ opcode::read::<u8>(GADGET_IOC_MAGIC, GADGET_GET_PRINTER_STATUS) }, u8>::new()
33 };
34 Ok(unsafe { ioctl::ioctl(file, getter) }?)
35}
36
37fn ioctl_write_printer_status(file: &File, status: u8) -> Result<()> {
38 let mut value = status;
39 unsafe {
40 ioctl::ioctl(
41 file,
42 ioctl::Updater::<{ opcode::read_write::<u8>(GADGET_IOC_MAGIC, GADGET_SET_PRINTER_STATUS) }, _>::new(
43 &mut value,
44 ),
45 )
46 }?;
47 Ok(())
48}
49
50fn create_printer_gadget() -> Result<RegGadget> {
51 usb_gadget::remove_all().expect("cannot remove all gadgets");
52
53 let udc = default_udc().expect("cannot get UDC");
54 let mut builder = Printer::builder();
55 builder.pnp_string = Some("Rust PNP".to_string());
56
57 let (_, func) = builder.build();
58 let reg = Gadget::new(
59 Class::INTERFACE_SPECIFIC,
60 Id::LINUX_FOUNDATION_COMPOSITE,
61 Strings::new("Clippy Manufacturer", "Rusty Printer", "RUST0123456"),
62 )
63 .with_config(Config::new("Config 1").with_function(func))
64 .bind(&udc)?;
65
66 Ok(reg)
67}
68
69fn read_printer_data(file: &mut File) -> Result<()> {
70 let mut buf = [0u8; BUF_SIZE];
71 let mut printed = 0;
72 println!("Will exit after printing {} pages...", PRINT_EXIT_COUNT);
73
74 loop {
75 let bytes_read = match file.read(&mut buf) {
76 Ok(bytes_read) if bytes_read > 0 => bytes_read,
77 _ => break,
78 };
79 io::stdout().write_all(&buf[..bytes_read])?;
80 io::stdout().flush()?;
81
82 // check if %%EOF is in the buffer
83 if buf.windows(5).any(|w| w == b"%%EOF") {
84 printed += 1;
85 if printed == PRINT_EXIT_COUNT {
86 println!("Printed {} pages, exiting.", PRINT_EXIT_COUNT);
87 break;
88 }
89 }
90 }
91
92 Ok(())
93}
94
95fn set_printer_status(file: &File, flags: StatusFlags, clear: bool) -> Result<StatusFlags> {
96 let mut status = get_printer_status(file)?;
97 if clear {
98 status.remove(flags);
99 } else {
100 status.insert(flags);
101 }
102 let bits = status.bits();
103 log::debug!("Setting printer status: {:08b}", bits);
104 ioctl_write_printer_status(file, bits)?;
105 Ok(StatusFlags::from_bits_truncate(bits))
106}
107
108fn get_printer_status(file: &File) -> Result<StatusFlags> {
109 let status = ioctl_read_printer_status(file)?;
110 log::debug!("Got printer status: {:08b}", status);
111 let status = StatusFlags::from_bits_truncate(status);
112 Ok(status)
113}Sourcepub const fn from_bits_retain(bits: u8) -> Self
pub const fn from_bits_retain(bits: u8) -> Self
Convert from a bits value exactly.
Sourcepub fn from_name(name: &str) -> Option<Self>
pub fn from_name(name: &str) -> Option<Self>
Get a flags value with the bits of a flag with the given name set.
This method will return None if name is empty or doesn’t
correspond to any named flag.
Sourcepub const fn intersects(&self, other: Self) -> bool
pub const fn intersects(&self, other: Self) -> bool
Whether any set bits in a source flags value are also set in a target flags value.
Sourcepub const fn contains(&self, other: Self) -> bool
pub const fn contains(&self, other: Self) -> bool
Whether all set bits in a source flags value are also set in a target flags value.
Examples found in repository?
115fn print_status(status: StatusFlags) {
116 println!("Printer status is:");
117 if status.contains(StatusFlags::SELECTED) {
118 println!(" Printer is Selected");
119 } else {
120 println!(" Printer is NOT Selected");
121 }
122 if status.contains(StatusFlags::PAPER_EMPTY) {
123 println!(" Paper is Out");
124 } else {
125 println!(" Paper is Loaded");
126 }
127 if status.contains(StatusFlags::NOT_ERROR) {
128 println!(" Printer OK");
129 } else {
130 println!(" Printer ERROR");
131 }
132}Sourcepub fn insert(&mut self, other: Self)
pub fn insert(&mut self, other: Self)
The bitwise or (|) of the bits in two flags values.
Examples found in repository?
95fn set_printer_status(file: &File, flags: StatusFlags, clear: bool) -> Result<StatusFlags> {
96 let mut status = get_printer_status(file)?;
97 if clear {
98 status.remove(flags);
99 } else {
100 status.insert(flags);
101 }
102 let bits = status.bits();
103 log::debug!("Setting printer status: {:08b}", bits);
104 ioctl_write_printer_status(file, bits)?;
105 Ok(StatusFlags::from_bits_truncate(bits))
106}Sourcepub fn remove(&mut self, other: Self)
pub fn remove(&mut self, other: Self)
The intersection of a source flags value with the complement of a target flags
value (&!).
This method is not equivalent to self & !other when other has unknown bits set.
remove won’t truncate other, but the ! operator will.
Examples found in repository?
95fn set_printer_status(file: &File, flags: StatusFlags, clear: bool) -> Result<StatusFlags> {
96 let mut status = get_printer_status(file)?;
97 if clear {
98 status.remove(flags);
99 } else {
100 status.insert(flags);
101 }
102 let bits = status.bits();
103 log::debug!("Setting printer status: {:08b}", bits);
104 ioctl_write_printer_status(file, bits)?;
105 Ok(StatusFlags::from_bits_truncate(bits))
106}Sourcepub fn toggle(&mut self, other: Self)
pub fn toggle(&mut self, other: Self)
The bitwise exclusive-or (^) of the bits in two flags values.
Sourcepub fn set(&mut self, other: Self, value: bool)
pub fn set(&mut self, other: Self, value: bool)
Call insert when value is true or remove when value is false.
Sourcepub const fn intersection(self, other: Self) -> Self
pub const fn intersection(self, other: Self) -> Self
The bitwise and (&) of the bits in two flags values.
Sourcepub const fn union(self, other: Self) -> Self
pub const fn union(self, other: Self) -> Self
The bitwise or (|) of the bits in two flags values.
Sourcepub const fn difference(self, other: Self) -> Self
pub const fn difference(self, other: Self) -> Self
The intersection of a source flags value with the complement of a target flags
value (&!).
This method is not equivalent to self & !other when other has unknown bits set.
difference won’t truncate other, but the ! operator will.
Sourcepub const fn symmetric_difference(self, other: Self) -> Self
pub const fn symmetric_difference(self, other: Self) -> Self
The bitwise exclusive-or (^) of the bits in two flags values.
Sourcepub const fn complement(self) -> Self
pub const fn complement(self) -> Self
The bitwise negation (!) of the bits in a flags value, truncating the result.
Source§impl StatusFlags
impl StatusFlags
Sourcepub const fn iter(&self) -> Iter<StatusFlags>
pub const fn iter(&self) -> Iter<StatusFlags>
Yield a set of contained flags values.
Each yielded flags value will correspond to a defined named flag. Any unknown bits will be yielded together as a final flags value.
Sourcepub const fn iter_names(&self) -> IterNames<StatusFlags>
pub const fn iter_names(&self) -> IterNames<StatusFlags>
Yield a set of contained named flags values.
This method is like iter, except only yields bits in contained named flags.
Any unknown bits, or bits not corresponding to a contained flag will not be yielded.
Trait Implementations§
Source§impl Binary for StatusFlags
impl Binary for StatusFlags
Source§impl BitAnd for StatusFlags
impl BitAnd for StatusFlags
Source§impl BitAndAssign for StatusFlags
impl BitAndAssign for StatusFlags
Source§fn bitand_assign(&mut self, other: Self)
fn bitand_assign(&mut self, other: Self)
The bitwise and (&) of the bits in two flags values.
Source§impl BitOr for StatusFlags
impl BitOr for StatusFlags
Source§fn bitor(self, other: StatusFlags) -> Self
fn bitor(self, other: StatusFlags) -> Self
The bitwise or (|) of the bits in two flags values.
Source§type Output = StatusFlags
type Output = StatusFlags
| operator.Source§impl BitOrAssign for StatusFlags
impl BitOrAssign for StatusFlags
Source§fn bitor_assign(&mut self, other: Self)
fn bitor_assign(&mut self, other: Self)
The bitwise or (|) of the bits in two flags values.
Source§impl BitXor for StatusFlags
impl BitXor for StatusFlags
Source§impl BitXorAssign for StatusFlags
impl BitXorAssign for StatusFlags
Source§fn bitxor_assign(&mut self, other: Self)
fn bitxor_assign(&mut self, other: Self)
The bitwise exclusive-or (^) of the bits in two flags values.
Source§impl Clone for StatusFlags
impl Clone for StatusFlags
Source§fn clone(&self) -> StatusFlags
fn clone(&self) -> StatusFlags
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for StatusFlags
impl Debug for StatusFlags
Source§impl Extend<StatusFlags> for StatusFlags
impl Extend<StatusFlags> for StatusFlags
Source§fn extend<T: IntoIterator<Item = Self>>(&mut self, iterator: T)
fn extend<T: IntoIterator<Item = Self>>(&mut self, iterator: T)
The bitwise or (|) of the bits in each flags value.
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl Flags for StatusFlags
impl Flags for StatusFlags
Source§const FLAGS: &'static [Flag<StatusFlags>]
const FLAGS: &'static [Flag<StatusFlags>]
Source§fn from_bits_retain(bits: u8) -> StatusFlags
fn from_bits_retain(bits: u8) -> StatusFlags
Source§fn known_bits(&self) -> Self::Bits
fn known_bits(&self) -> Self::Bits
Source§fn unknown_bits(&self) -> Self::Bits
fn unknown_bits(&self) -> Self::Bits
Source§fn contains_unknown_bits(&self) -> bool
fn contains_unknown_bits(&self) -> bool
true if any unknown bits are set.Source§fn from_bits_truncate(bits: Self::Bits) -> Self
fn from_bits_truncate(bits: Self::Bits) -> Self
Source§fn from_name(name: &str) -> Option<Self>
fn from_name(name: &str) -> Option<Self>
Source§fn iter_names(&self) -> IterNames<Self>
fn iter_names(&self) -> IterNames<Self>
Source§fn iter_defined_names() -> IterDefinedNames<Self>
fn iter_defined_names() -> IterDefinedNames<Self>
Self::FLAGS.Source§fn intersects(&self, other: Self) -> boolwhere
Self: Sized,
fn intersects(&self, other: Self) -> boolwhere
Self: Sized,
Source§fn contains(&self, other: Self) -> boolwhere
Self: Sized,
fn contains(&self, other: Self) -> boolwhere
Self: Sized,
Source§fn insert(&mut self, other: Self)where
Self: Sized,
fn insert(&mut self, other: Self)where
Self: Sized,
|) of the bits in two flags values.Source§fn remove(&mut self, other: Self)where
Self: Sized,
fn remove(&mut self, other: Self)where
Self: Sized,
&!). Read moreSource§fn toggle(&mut self, other: Self)where
Self: Sized,
fn toggle(&mut self, other: Self)where
Self: Sized,
^) of the bits in two flags values.Source§fn intersection(self, other: Self) -> Self
fn intersection(self, other: Self) -> Self
&) of the bits in two flags values.Source§fn difference(self, other: Self) -> Self
fn difference(self, other: Self) -> Self
&!). Read moreSource§fn symmetric_difference(self, other: Self) -> Self
fn symmetric_difference(self, other: Self) -> Self
^) of the bits in two flags values.Source§fn complement(self) -> Self
fn complement(self) -> Self
!) of the bits in a flags value, truncating the result.Source§impl FromIterator<StatusFlags> for StatusFlags
impl FromIterator<StatusFlags> for StatusFlags
Source§fn from_iter<T: IntoIterator<Item = Self>>(iterator: T) -> Self
fn from_iter<T: IntoIterator<Item = Self>>(iterator: T) -> Self
The bitwise or (|) of the bits in each flags value.
Source§impl IntoIterator for StatusFlags
impl IntoIterator for StatusFlags
Source§impl LowerHex for StatusFlags
impl LowerHex for StatusFlags
Source§impl Not for StatusFlags
impl Not for StatusFlags
Source§impl Octal for StatusFlags
impl Octal for StatusFlags
Source§impl PublicFlags for StatusFlags
impl PublicFlags for StatusFlags
Source§impl Sub for StatusFlags
impl Sub for StatusFlags
Source§fn sub(self, other: Self) -> Self
fn sub(self, other: Self) -> Self
The intersection of a source flags value with the complement of a target flags value (&!).
This method is not equivalent to self & !other when other has unknown bits set.
difference won’t truncate other, but the ! operator will.
Source§type Output = StatusFlags
type Output = StatusFlags
- operator.Source§impl SubAssign for StatusFlags
impl SubAssign for StatusFlags
Source§fn sub_assign(&mut self, other: Self)
fn sub_assign(&mut self, other: Self)
The intersection of a source flags value with the complement of a target flags value (&!).
This method is not equivalent to self & !other when other has unknown bits set.
difference won’t truncate other, but the ! operator will.