Merge pull request #4084 from iliana/cyw43-state-const-new

cyw43: make State::new a const fn
This commit is contained in:
Dario Nieuwenhuis 2025-04-13 17:09:35 +00:00 committed by GitHub
commit dd3ae39d42
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 25 additions and 10 deletions

View File

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

View File

@ -33,8 +33,8 @@ struct Wakers {
runner: WakerRegistration,
}
impl Default for Wakers {
fn default() -> Self {
impl Wakers {
const fn new() -> Self {
Self {
control: WakerRegistration::new(),
runner: WakerRegistration::new(),
@ -48,10 +48,10 @@ pub struct IoctlState {
}
impl IoctlState {
pub fn new() -> Self {
pub const fn new() -> Self {
Self {
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 {
/// Create new driver state holder.
pub fn new() -> Self {
pub const fn new() -> Self {
Self {
ioctl_state: IoctlState::new(),
net: NetState {