cyw43: make State::new a const fn

This commit is contained in:
iliana etaoin 2025-04-13 00:26:43 -07:00
parent 659c616f9f
commit db83d7275e
3 changed files with 25 additions and 10 deletions

View File

@ -296,10 +296,10 @@ pub struct Events {
} }
impl Events { impl Events {
pub fn new() -> Self { pub const fn new() -> Self {
Self { Self {
queue: EventQueue::new(), queue: EventQueue::new(),
mask: SharedEventMask::default(), mask: SharedEventMask::new(),
} }
} }
} }
@ -333,7 +333,6 @@ impl Message {
} }
} }
#[derive(Default)]
struct EventMask { struct EventMask {
mask: [u32; Self::WORD_COUNT], mask: [u32; Self::WORD_COUNT],
} }
@ -341,6 +340,12 @@ struct EventMask {
impl EventMask { impl EventMask {
const WORD_COUNT: usize = ((Event::LAST as u32 + (u32::BITS - 1)) / u32::BITS) as usize; const WORD_COUNT: usize = ((Event::LAST as u32 + (u32::BITS - 1)) / u32::BITS) as usize;
const fn new() -> Self {
Self {
mask: [0; Self::WORD_COUNT],
}
}
fn enable(&mut self, event: Event) { fn enable(&mut self, event: Event) {
let n = event as u32; let n = event as u32;
let word = n / u32::BITS; let word = n / u32::BITS;
@ -366,13 +371,17 @@ impl EventMask {
} }
} }
#[derive(Default)]
pub struct SharedEventMask { pub struct SharedEventMask {
mask: RefCell<EventMask>, mask: RefCell<EventMask>,
} }
impl SharedEventMask { impl SharedEventMask {
pub const fn new() -> Self {
Self {
mask: RefCell::new(EventMask::new()),
}
}
pub fn enable(&self, events: &[Event]) { pub fn enable(&self, events: &[Event]) {
let mut mask = self.mask.borrow_mut(); let mut mask = self.mask.borrow_mut();
for event in events { for event in events {
@ -398,3 +407,9 @@ impl SharedEventMask {
mask.is_enabled(event) mask.is_enabled(event)
} }
} }
impl Default for SharedEventMask {
fn default() -> Self {
Self::new()
}
}

View File

@ -33,8 +33,8 @@ struct Wakers {
runner: WakerRegistration, runner: WakerRegistration,
} }
impl Default for Wakers { impl Wakers {
fn default() -> Self { const fn new() -> Self {
Self { Self {
control: WakerRegistration::new(), control: WakerRegistration::new(),
runner: WakerRegistration::new(), runner: WakerRegistration::new(),
@ -48,10 +48,10 @@ pub struct IoctlState {
} }
impl IoctlState { impl IoctlState {
pub fn new() -> Self { pub const fn new() -> Self {
Self { Self {
state: Cell::new(IoctlStateInner::Done { resp_len: 0 }), state: Cell::new(IoctlStateInner::Done { resp_len: 0 }),
wakers: Default::default(), wakers: RefCell::new(Wakers::new()),
} }
} }

View File

@ -124,7 +124,7 @@ struct NetState {
impl State { impl State {
/// Create new driver state holder. /// Create new driver state holder.
pub fn new() -> Self { pub const fn new() -> Self {
Self { Self {
ioctl_state: IoctlState::new(), ioctl_state: IoctlState::new(),
net: NetState { net: NetState {