Skip to content

Commit

Permalink
Simplifying trait, sealing implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan-summers authored and kellerkindt committed Jul 5, 2024
1 parent ec57a2f commit 270436a
Showing 1 changed file with 8 additions and 15 deletions.
23 changes: 8 additions & 15 deletions src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@ impl<E> From<E> for ResetError<E> {
}
}

pub trait State {
type Host: Host;
mod private {
pub trait Sealed {}

impl<'a, T: Sealed> Sealed for &'a mut T {}
}

pub trait State: private::Sealed {
fn socket(&mut self) -> Option<Socket>;
fn release_socket(&mut self, socket: Socket);
fn any_allocated(&self) -> bool;
fn host(&self) -> &Self::Host;
}

#[derive(Debug)]
Expand All @@ -48,9 +51,9 @@ impl<HostImpl: Host> DeviceState<HostImpl> {
}
}

impl<HostImpl: Host> State for DeviceState<HostImpl> {
type Host = HostImpl;
impl<HostImpl: Host> private::Sealed for DeviceState<HostImpl> {}

impl<HostImpl: Host> State for DeviceState<HostImpl> {
fn socket(&mut self) -> Option<Socket> {
for index in 0..8 {
if self.sockets.get_bit(index) {
Expand All @@ -65,18 +68,12 @@ impl<HostImpl: Host> State for DeviceState<HostImpl> {
self.sockets.set_bit(socket.index.into(), true);
}

fn host(&self) -> &Self::Host {
&self.host
}

fn any_allocated(&self) -> bool {
self.sockets != 0xFF
}
}

impl<'a, T: State> State for &'a mut T {
type Host = T::Host;

fn socket(&mut self) -> Option<Socket> {
T::socket(self)
}
Expand All @@ -85,10 +82,6 @@ impl<'a, T: State> State for &'a mut T {
T::release_socket(self, socket)
}

fn host(&self) -> &Self::Host {
T::host(self)
}

fn any_allocated(&self) -> bool {
T::any_allocated(self)
}
Expand Down

0 comments on commit 270436a

Please sign in to comment.