Skip to content

Commit

Permalink
Use derive-where instead of derivative
Browse files Browse the repository at this point in the history
The derivative crate is dead: mcarton/rust-derivative#117.
  • Loading branch information
ia0 committed Aug 13, 2024
1 parent 9293f9c commit 197f4c1
Show file tree
Hide file tree
Showing 15 changed files with 37 additions and 151 deletions.
1 change: 1 addition & 0 deletions crates/board/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

### Patch

- Use `derive-where` instead of `derivative`
- Update dependencies
- Remove workaround lint false positive

Expand Down
10 changes: 5 additions & 5 deletions crates/board/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/board/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ bytemuck = { version = "1.16.0", default-features = false, optional = true }
ccm = { version = "0.5.0", default-features = false, optional = true }
crypto-common = { version = "0.1.6", default-features = false, optional = true }
defmt = { version = "0.3.8", default-features = false, optional = true }
derivative = { version = "2.2.0", default-features = false, features = ["use_core"] }
derive-where = { version = "1.2.7", default-features = false, features = ["nightly"] }
digest = { version = "0.10.7", default-features = false, optional = true }
ecdsa = { version = "0.16.9", default-features = false, optional = true }
elliptic-curve = { version = "0.13.8", default-features = false, optional = true }
Expand Down
5 changes: 2 additions & 3 deletions crates/board/src/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@
//! A button is an input interface with 2 states: pressed and released. Buttons must support
//! triggering events when changing state. Events may be enabled or disabled per button.

use derivative::Derivative;
use derive_where::derive_where;

use crate::{Error, Id, Support};

/// Button event.
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(Derivative)]
#[derivative(Debug(bound = ""), PartialEq(bound = ""), Eq(bound = ""))]
#[derive_where(Debug, PartialEq, Eq)]
pub struct Event<B: crate::Api + ?Sized> {
/// The button that triggered the event.
pub button: Id<crate::Button<B>>,
Expand Down
47 changes: 5 additions & 42 deletions crates/board/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ extern crate alloc;
use core::marker::PhantomData;
use core::ops::Deref;

use derivative::Derivative;
use derive_where::derive_where;
use wasefire_error::Code;
pub use wasefire_error::Error;

Expand Down Expand Up @@ -151,8 +151,7 @@ pub trait Singleton: Sized {
/// Events are de-duplicated if the previous one was not processed yet, because some events may
/// trigger repeatedly.
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(Derivative)]
#[derivative(Debug(bound = ""), PartialEq(bound = ""), Eq(bound = ""))]
#[derive_where(Debug, PartialEq, Eq)]
pub enum Event<B: Api + ?Sized> {
/// Button event.
#[cfg(feature = "api-button")]
Expand Down Expand Up @@ -186,11 +185,8 @@ pub enum Event<B: Api + ?Sized> {
///
/// This type is useful when the type parameter `B` needs to be mentioned in an enum. This type can
/// be destructed by calling its unreachable method.
#[derive(Derivative)]
#[derivative(Debug(bound = ""), Copy(bound = ""), Hash(bound = ""))]
#[derivative(PartialEq(bound = ""), Eq(bound = ""), Ord(bound = ""))]
#[derivative(Ord = "feature_allow_slow_enum")]
pub struct Impossible<B: Api + ?Sized>(Void, PhantomData<B>);
#[derive_where(Debug, Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct Impossible<B: Api + ?Sized>(!, PhantomData<B>);

#[cfg(feature = "defmt")]
impl<B: Api + ?Sized> defmt::Format for Impossible<B> {
Expand All @@ -199,30 +195,13 @@ impl<B: Api + ?Sized> defmt::Format for Impossible<B> {
}
}

// TODO(https://github.com/mcarton/rust-derivative/issues/112): Use Clone(bound = "") instead.
impl<B: Api + ?Sized> Clone for Impossible<B> {
fn clone(&self) -> Self {
*self
}
}

// TODO(https://github.com/mcarton/rust-derivative/issues/112): Use PartialOrd(bound = "") instead.
impl<B: Api + ?Sized> PartialOrd for Impossible<B> {
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
Some(self.cmp(other))
}
}

impl<B: Api + ?Sized> Impossible<B> {
/// Provides a static proof of dead code.
pub fn unreachable(&self) -> ! {
match self.0 {}
}
}

#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
enum Void {}

/// Button interface.
#[cfg(feature = "api-button")]
pub type Button<B> = <B as Api>::Button;
Expand Down Expand Up @@ -271,9 +250,7 @@ pub type Uart<B> = <B as Api>::Uart;
pub type Usb<B> = <B as Api>::Usb;

/// Valid identifier for a countable API.
#[derive(Derivative)]
#[derivative(Debug(bound = ""), Copy(bound = ""), Hash(bound = ""))]
#[derivative(PartialEq(bound = ""), Eq(bound = ""), Ord(bound = ""))]
#[derive_where(Debug, Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct Id<T: Support<usize> + ?Sized> {
// Invariant: value < T::SUPPORT
value: usize,
Expand All @@ -287,20 +264,6 @@ impl<T: Support<usize> + ?Sized> defmt::Format for Id<T> {
}
}

// TODO(https://github.com/mcarton/rust-derivative/issues/112): Use Clone(bound = "") instead.
impl<T: Support<usize> + ?Sized> Clone for Id<T> {
fn clone(&self) -> Self {
*self
}
}

// TODO(https://github.com/mcarton/rust-derivative/issues/112): Use PartialOrd(bound = "") instead.
impl<T: Support<usize> + ?Sized> PartialOrd for Id<T> {
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
Some(self.cmp(other))
}
}

impl<T: Support<usize>> Id<T> {
/// Creates a safe identifier for an API with countable support.
pub fn new(value: usize) -> Result<Self, Error> {
Expand Down
5 changes: 2 additions & 3 deletions crates/board/src/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@
//!
//! A timer triggers an event after a given amount of time (possibly periodically).

use derivative::Derivative;
use derive_where::derive_where;

use crate::{Error, Id, Support};

/// Timer event.
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(Derivative)]
#[derivative(Debug(bound = ""), PartialEq(bound = ""), Eq(bound = ""))]
#[derive_where(Debug, PartialEq, Eq)]
pub struct Event<B: crate::Api + ?Sized> {
/// The timer that triggered the event.
pub timer: Id<crate::Timer<B>>,
Expand Down
5 changes: 2 additions & 3 deletions crates/board/src/uart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@

//! UART interface.

use derivative::Derivative;
use derive_where::derive_where;

use crate::{Error, Id, Support};

/// UART event.
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(Derivative)]
#[derivative(Debug(bound = ""), PartialEq(bound = ""), Eq(bound = ""))]
#[derive_where(Debug, PartialEq, Eq)]
pub struct Event<B: crate::Api + ?Sized> {
/// The UART that triggered the event.
pub uart: Id<crate::Uart<B>>,
Expand Down
1 change: 1 addition & 0 deletions crates/scheduler/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Patch

- Use `derive-where` instead of `derivative`
- Implement `defmt::Format` for `Key` when `defmt` is enabled
- Stop using `log::Debug2Format()` when logging events
- Make applet optional
Expand Down
12 changes: 6 additions & 6 deletions crates/scheduler/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/scheduler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ features = ["std", "wasm"]
[dependencies]
bytemuck = { version = "1.16.0", default-features = false }
defmt = { version = "0.3.8", default-features = false, optional = true }
derivative = { version = "2.2.0", default-features = false, features = ["use_core"] }
derive-where = { version = "1.2.7", default-features = false, features = ["nightly"] }
digest = { version = "0.10.7", default-features = false, features = ["mac"], optional = true }
generic-array = { version = "=0.14.7", default-features = false, optional = true }
typenum = { version = "1.17.0", default-features = false, optional = true }
Expand Down
33 changes: 3 additions & 30 deletions crates/scheduler/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use alloc::vec;
use core::borrow::Borrow;

use derivative::Derivative;
use derive_where::derive_where;
use wasefire_board_api::{Api as Board, Event, Impossible};
#[cfg(feature = "wasm")]
pub use wasefire_interpreter::InstId;
Expand All @@ -42,10 +42,7 @@ pub struct InstId;

// TODO: This could be encoded into a u32 for performance/footprint.
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(Derivative)]
#[derivative(Debug(bound = ""), Copy(bound = ""), Hash(bound = ""))]
#[derivative(PartialEq(bound = ""), Eq(bound = ""), Ord(bound = ""))]
#[derivative(Ord = "feature_allow_slow_enum")]
#[derive_where(Debug, Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub enum Key<B: Board> {
#[cfg(feature = "board-api-button")]
Button(button::Key<B>),
Expand All @@ -62,20 +59,6 @@ pub enum Key<B: Board> {
_Impossible(Impossible<B>),
}

// TODO(https://github.com/mcarton/rust-derivative/issues/112): Use Clone(bound = "") instead.
impl<B: Board> Clone for Key<B> {
fn clone(&self) -> Self {
*self
}
}

// TODO(https://github.com/mcarton/rust-derivative/issues/112): Use PartialOrd(bound = "") instead.
impl<B: Board> PartialOrd for Key<B> {
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
Some(self.cmp(other))
}
}

#[cfg_attr(not(feature = "board-api-button"), allow(unused_macros))]
macro_rules! or_unreachable {
($feature:literal, [$($event:ident)*], $expr:expr) => {{
Expand Down Expand Up @@ -123,24 +106,14 @@ impl<'a, B: Board> From<&'a Event<B>> for Key<B> {
}
}

#[derive(Derivative)]
#[derivative(Debug(bound = ""), Clone(bound = ""))]
#[derivative(PartialEq(bound = ""), Eq(bound = ""), Ord(bound = ""))]
#[derivative(Ord = "feature_allow_slow_enum")]
#[derive_where(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct Handler<B: Board> {
pub key: Key<B>,
pub inst: InstId,
pub func: u32,
pub data: u32,
}

// TODO(https://github.com/mcarton/rust-derivative/issues/112): Use PartialOrd(bound = "") instead.
impl<B: Board> PartialOrd for Handler<B> {
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
Some(self.cmp(other))
}
}

impl<B: Board> Borrow<Key<B>> for Handler<B> {
fn borrow(&self) -> &Key<B> {
&self.key
Expand Down
20 changes: 2 additions & 18 deletions crates/scheduler/src/event/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,16 @@

use alloc::vec::Vec;

use derivative::Derivative;
use derive_where::derive_where;
use wasefire_board_api::button::Event;
use wasefire_board_api::{self as board, Api as Board, Id};

#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(Derivative)]
#[derivative(Debug(bound = ""), Copy(bound = ""), Hash(bound = ""))]
#[derivative(PartialEq(bound = ""), Eq(bound = ""), Ord(bound = ""))]
#[derive_where(Debug, Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct Key<B: Board> {
pub button: Id<board::Button<B>>,
}

// TODO(https://github.com/mcarton/rust-derivative/issues/112): Use Clone(bound = "") instead.
impl<B: Board> Clone for Key<B> {
fn clone(&self) -> Self {
*self
}
}

// TODO(https://github.com/mcarton/rust-derivative/issues/112): Use PartialOrd(bound = "") instead.
impl<B: Board> PartialOrd for Key<B> {
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
Some(self.cmp(other))
}
}

impl<B: Board> From<Key<B>> for crate::event::Key<B> {
fn from(key: Key<B>) -> Self {
crate::event::Key::Button(key)
Expand Down
20 changes: 2 additions & 18 deletions crates/scheduler/src/event/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,16 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use derivative::Derivative;
use derive_where::derive_where;
use wasefire_board_api::timer::Event;
use wasefire_board_api::{self as board, Api as Board, Id};

#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(Derivative)]
#[derivative(Debug(bound = ""), Copy(bound = ""), Hash(bound = ""))]
#[derivative(PartialEq(bound = ""), Eq(bound = ""), Ord(bound = ""))]
#[derive_where(Debug, Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct Key<B: Board> {
pub timer: Id<board::Timer<B>>,
}

// TODO(https://github.com/mcarton/rust-derivative/issues/112): Use Clone(bound = "") instead.
impl<B: Board> Clone for Key<B> {
fn clone(&self) -> Self {
*self
}
}

// TODO(https://github.com/mcarton/rust-derivative/issues/112): Use PartialOrd(bound = "") instead.
impl<B: Board> PartialOrd for Key<B> {
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
Some(self.cmp(other))
}
}

impl<B: Board> From<Key<B>> for crate::event::Key<B> {
fn from(key: Key<B>) -> Self {
crate::event::Key::Timer(key)
Expand Down
Loading

0 comments on commit 197f4c1

Please sign in to comment.