From 270436affa03b54f0db531b506379f86c5b3f81a Mon Sep 17 00:00:00 2001 From: Ryan Summers Date: Fri, 5 Jul 2024 13:27:39 +0200 Subject: [PATCH] Simplifying trait, sealing implementations --- src/device.rs | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/src/device.rs b/src/device.rs index 15cb749..ba957d2 100644 --- a/src/device.rs +++ b/src/device.rs @@ -23,13 +23,16 @@ impl From for ResetError { } } -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; fn release_socket(&mut self, socket: Socket); fn any_allocated(&self) -> bool; - fn host(&self) -> &Self::Host; } #[derive(Debug)] @@ -48,9 +51,9 @@ impl DeviceState { } } -impl State for DeviceState { - type Host = HostImpl; +impl private::Sealed for DeviceState {} +impl State for DeviceState { fn socket(&mut self) -> Option { for index in 0..8 { if self.sockets.get_bit(index) { @@ -65,18 +68,12 @@ impl State for DeviceState { 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 { T::socket(self) } @@ -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) }