Skip to content

Commit

Permalink
fix!(identity): Remove DeviceId::Error in favor of RPITIT
Browse files Browse the repository at this point in the history
  • Loading branch information
chrysn committed Oct 17, 2024
1 parent a2bb8ab commit f9a0034
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 20 deletions.
20 changes: 6 additions & 14 deletions src/riot-rs-embassy-common/src/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,6 @@
/// evolve at the (possibly faster) RIOT-rs internal speed of `riot-rs-embassy-common` for
/// implementers.
pub trait DeviceId: Sized + core::fmt::Debug + defmt::Format + crate::Sealed {
/// Error type indicating that no identifier is available.
///
/// This is part of the return type of the [`Self::get()`] constructor.
///
/// It is encouraged to be [`core::convert::Infallible`] where possible.
type Error: Error;

/// Some `[u8; N]` type, returned by [`.bytes()`][Self::bytes].
///
/// This may not represent all the identifying information available on the board, but can
Expand All @@ -54,16 +47,17 @@ pub trait DeviceId: Sized + core::fmt::Debug + defmt::Format + crate::Sealed {
/// # Errors
///
/// This produces an error if no device ID is available on this board, or is not implemented.
fn get() -> Result<Self, Self::Error>;
/// It is encouraged to use [`core::convert::Infallible`] where possible.
fn get() -> Result<Self, impl Error>;

/// The device identifier in serialized bytes format.
fn bytes(&self) -> Self::Bytes;
}

/// Error trait for obtaining [`DeviceId`].
///
/// This type exists to ease the evolution of [`DeviceId::Error`], and to make it expressible in
/// other crates without duplicating the requirements set.
/// This is part of the signature of [`DeviceId::get`], and indicates that no identifier is
/// available.
///
/// Like [`DeviceId`], it is sealed; the same considerations for stability as listed there apply.
pub trait Error: core::error::Error + defmt::Format + crate::Sealed {}
Expand All @@ -83,14 +77,12 @@ pub struct NoDeviceId<E: Error + Default>(core::convert::Infallible, core::marke
impl<E: Error + Default> crate::Sealed for NoDeviceId<E> {}

impl<E: Error + Default> DeviceId for NoDeviceId<E> {
type Error = E;

// We could also come up with a custom never type that AsRef's into [u8], but that won't fly
// once there is a BYTES_LEN.
type Bytes = [u8; 0];

fn get() -> Result<Self, Self::Error> {
Err(Default::default())
fn get() -> Result<Self, impl Error> {
Err::<_, E>(Default::default())
}

fn bytes(&self) -> [u8; 0] {
Expand Down
8 changes: 5 additions & 3 deletions src/riot-rs-nrf/src/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ pub struct DeviceId(u64);
impl riot_rs_embassy_common::Sealed for DeviceId {}

impl riot_rs_embassy_common::identity::DeviceId for DeviceId {
type Error = core::convert::Infallible;

fn get() -> Result<Self, Self::Error> {
#[allow(
refining_impl_trait_reachable,
reason = "Making this fallible would be a breaking API change for RIOT-rs."
)]
fn get() -> Result<Self, core::convert::Infallible> {
// Embassy does not wrap the FICR register, and given that all we need from there is a register
// read that is perfectly fine to do through a stolen register, let's do that rather than
// thread the access through several layers.
Expand Down
8 changes: 5 additions & 3 deletions src/riot-rs-stm32/src/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ impl riot_rs_embassy_common::Sealed for DeviceId {}
impl riot_rs_embassy_common::identity::DeviceId for DeviceId {
type Bytes = &'static [u8; 12];

type Error = core::convert::Infallible;

fn get() -> Result<Self, Self::Error> {
#[allow(
refining_impl_trait_reachable,
reason = "Making this fallible would be a breaking API change for RIOT-rs."
)]
fn get() -> Result<Self, core::convert::Infallible> {
Ok(Self(embassy_stm32::uid::uid()))
}

Expand Down

0 comments on commit f9a0034

Please sign in to comment.