Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for non-zero integers. #288

Merged
merged 2 commits into from
May 13, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion 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 All @@ -795,8 +813,12 @@ mod tests {

#[track_caller]
fn fm<T: FromMeta>(tokens: TokenStream) -> T {
try_fm(tokens).expect("Tests should pass valid input")
}

#[track_caller]
fn try_fm<T: FromMeta>(tokens: TokenStream) -> Result<T> {
TedDriggs marked this conversation as resolved.
Show resolved Hide resolved
FromMeta::from_meta(&pm(tokens).expect("Tests should pass well-formed input"))
.expect("Tests should pass valid input")
}

#[test]
Expand Down Expand Up @@ -853,6 +875,22 @@ mod tests {
assert_eq!(fm::<f64>(quote!(ignore = "1.4e10")), 1.4e10);
}

#[test]
fn nonzero_number_fails() {
assert_eq!(
try_fm::<NonZeroU64>(quote!(ignore = "0")),
Err(Error::unknown_value("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