Skip to content

Commit

Permalink
add derive macro for trait Unsigned
Browse files Browse the repository at this point in the history
  • Loading branch information
tdelabro committed Sep 22, 2023
1 parent f1e5005 commit 0a8f903
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -992,3 +992,21 @@ pub fn signed(input: TokenStream) -> TokenStream {

import.wrap("Signed", &name, impl_).into()
}

/// Derives [`num_traits::Unsigned`][unsigned]. The inner type must already implement
/// `Unsigned`.
///
/// [unsigned]: https://docs.rs/num/latest/num/traits/trait.Unsigned.html
#[proc_macro_derive(Unsigned, attributes(num_traits))]
pub fn unsigned(input: TokenStream) -> TokenStream {
let ast = parse!(input as syn::DeriveInput);
let name = &ast.ident;

let import = NumTraits::new(&ast);

let impl_ = quote! {
impl #import::Unsigned for #name {}
};

import.wrap("Unsigned", &name, impl_).into()
}
15 changes: 14 additions & 1 deletion tests/newtype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,22 @@ extern crate num as num_renamed;
#[macro_use]
extern crate num_derive;

use crate::num_renamed::{Float, FromPrimitive, Num, NumCast, One, Signed, ToPrimitive, Zero};
use crate::num_renamed::{
Float, FromPrimitive, Num, NumCast, One, Signed, ToPrimitive, Unsigned, Zero,
};
use std::ops::Neg;

#[derive(PartialEq, Zero, One, NumOps, Num, Unsigned)]
struct MyNum(u32);

#[test]
fn test_derive_unsingned_works() {
fn do_nothing_on_unsigned(_input: impl Unsigned) {}

let x = MyNum(42);
do_nothing_on_unsigned(x);
}

#[derive(
Debug,
Clone,
Expand Down

0 comments on commit 0a8f903

Please sign in to comment.