Skip to content

Commit

Permalink
Add support for non-zero integers. (#288)
Browse files Browse the repository at this point in the history
  • Loading branch information
mpapierski committed May 13, 2024
1 parent e44e043 commit ba363b8
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions core/src/from_meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ use std::cell::RefCell;
use std::collections::hash_map::HashMap;
use std::collections::HashSet;
use std::hash::BuildHasher;
use std::num::{
NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroIsize, NonZeroU128,
NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize,
};
use std::rc::Rc;
use std::sync::atomic::AtomicBool;
use std::sync::Arc;
Expand Down Expand Up @@ -249,6 +253,18 @@ from_meta_num!(i32);
from_meta_num!(i64);
from_meta_num!(i128);
from_meta_num!(isize);
from_meta_num!(NonZeroU8);
from_meta_num!(NonZeroU16);
from_meta_num!(NonZeroU32);
from_meta_num!(NonZeroU64);
from_meta_num!(NonZeroU128);
from_meta_num!(NonZeroUsize);
from_meta_num!(NonZeroI8);
from_meta_num!(NonZeroI16);
from_meta_num!(NonZeroI32);
from_meta_num!(NonZeroI64);
from_meta_num!(NonZeroI128);
from_meta_num!(NonZeroIsize);

/// Generate an impl of `FromMeta` that will accept strings which parse to floats or
/// float literals.
Expand Down Expand Up @@ -781,6 +797,8 @@ hash_map!(syn::Path);
/// it should not be considered by the parsing.
#[cfg(test)]
mod tests {
use std::num::{NonZeroU32, NonZeroU64};

use proc_macro2::TokenStream;
use quote::quote;
use syn::parse_quote;
Expand Down Expand Up @@ -853,6 +871,20 @@ mod tests {
assert_eq!(fm::<f64>(quote!(ignore = "1.4e10")), 1.4e10);
}

#[should_panic(expected = "UnknownValue(\"0\")")]
#[test]
fn nonzero_number_fails() {
fm::<NonZeroU64>(quote!(ignore = "0"));
}

#[test]
fn nonzero_number_succeeds() {
assert_eq!(
fm::<NonZeroU32>(quote!(ignore = "2")),
NonZeroU32::new(2).unwrap()
);
}

#[test]
fn int_without_quotes() {
assert_eq!(fm::<u8>(quote!(ignore = 2)), 2u8);
Expand Down

0 comments on commit ba363b8

Please sign in to comment.