Skip to content

Commit 0a8f903

Browse files
committed
add derive macro for trait Unsigned
1 parent f1e5005 commit 0a8f903

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/lib.rs

+18
Original file line numberDiff line numberDiff line change
@@ -992,3 +992,21 @@ pub fn signed(input: TokenStream) -> TokenStream {
992992

993993
import.wrap("Signed", &name, impl_).into()
994994
}
995+
996+
/// Derives [`num_traits::Unsigned`][unsigned]. The inner type must already implement
997+
/// `Unsigned`.
998+
///
999+
/// [unsigned]: https://docs.rs/num/latest/num/traits/trait.Unsigned.html
1000+
#[proc_macro_derive(Unsigned, attributes(num_traits))]
1001+
pub fn unsigned(input: TokenStream) -> TokenStream {
1002+
let ast = parse!(input as syn::DeriveInput);
1003+
let name = &ast.ident;
1004+
1005+
let import = NumTraits::new(&ast);
1006+
1007+
let impl_ = quote! {
1008+
impl #import::Unsigned for #name {}
1009+
};
1010+
1011+
import.wrap("Unsigned", &name, impl_).into()
1012+
}

tests/newtype.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,22 @@ extern crate num as num_renamed;
22
#[macro_use]
33
extern crate num_derive;
44

5-
use crate::num_renamed::{Float, FromPrimitive, Num, NumCast, One, Signed, ToPrimitive, Zero};
5+
use crate::num_renamed::{
6+
Float, FromPrimitive, Num, NumCast, One, Signed, ToPrimitive, Unsigned, Zero,
7+
};
68
use std::ops::Neg;
79

10+
#[derive(PartialEq, Zero, One, NumOps, Num, Unsigned)]
11+
struct MyNum(u32);
12+
13+
#[test]
14+
fn test_derive_unsingned_works() {
15+
fn do_nothing_on_unsigned(_input: impl Unsigned) {}
16+
17+
let x = MyNum(42);
18+
do_nothing_on_unsigned(x);
19+
}
20+
821
#[derive(
922
Debug,
1023
Clone,

0 commit comments

Comments
 (0)