Skip to content

Commit

Permalink
Merge pull request #183 from vic1707/fix-std-instead-of-core-or-alloc
Browse files Browse the repository at this point in the history
Fix std instead of core or alloc
  • Loading branch information
greyblake authored Nov 8, 2024
2 parents 7f6e619 + 573740d commit 87b51c5
Show file tree
Hide file tree
Showing 18 changed files with 54 additions and 46 deletions.
5 changes: 5 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[build]
rustflags = [
"-Dclippy::std_instead_of_core",
"-Dclippy::std_instead_of_alloc",
]
5 changes: 3 additions & 2 deletions examples/any_generics/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use nutype::nutype;
use std::borrow::Cow;
use std::cmp::Ord;
extern crate alloc;
use alloc::borrow::Cow;
use core::cmp::Ord;

/// A wrapper around a vector that is guaranteed to be sorted.
#[nutype(
Expand Down
2 changes: 1 addition & 1 deletion nutype_macros/src/any/models.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use kinded::Kinded;
use proc_macro2::TokenStream;
use quote::ToTokens;
use std::fmt::Debug;
use core::fmt::Debug;
use syn::Field;

use crate::common::models::{CustomFunction, Guard, RawGuard, SpannedItem, TypeTrait};
Expand Down
3 changes: 2 additions & 1 deletion nutype_macros/src/common/gen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ pub mod parse_error;
pub mod tests;
pub mod traits;

use std::{collections::HashSet, hash::Hash};
use core::hash::Hash;
use std::collections::HashSet;

use self::traits::GeneratedTraits;

Expand Down
4 changes: 2 additions & 2 deletions nutype_macros/src/common/gen/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,8 @@ pub fn gen_impl_trait_serde_deserialize(
impl #all_generics_with_deserialize_bound ::serde::Deserialize<'de> for #type_name #type_generics_without_bounds {
fn deserialize<D: ::serde::Deserializer<'de>>(deserializer: D) -> ::core::result::Result<Self, D::Error> {
struct __Visitor #all_generics {
marker: ::std::marker::PhantomData<#type_name #type_generics_without_bounds>,
lifetime: ::std::marker::PhantomData<&'de ()>,
marker: ::core::marker::PhantomData<#type_name #type_generics_without_bounds>,
lifetime: ::core::marker::PhantomData<&'de ()>,
}

impl #all_generics_with_deserialize_bound ::serde::de::Visitor<'de> for __Visitor #all_generics_without_bounds {
Expand Down
4 changes: 2 additions & 2 deletions nutype_macros/src/common/models.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
mod error_type_path;

use kinded::Kinded;
use std::ops::Add;
use std::{collections::HashSet, fmt::Debug};
use core::{fmt::Debug, ops::Add};
use std::collections::HashSet;
use syn::Generics;

use proc_macro2::{Span, TokenStream};
Expand Down
10 changes: 5 additions & 5 deletions nutype_macros/src/common/parse/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub mod derive_trait;
pub mod meta;

use std::{
use core::{
any::type_name,
fmt::{Debug, Display},
str::FromStr,
Expand Down Expand Up @@ -364,7 +364,7 @@ where
pub fn parse_typed_custom_function<T>(
input: ParseStream,
) -> syn::Result<(TypedCustomFunction, Span)> {
let tp_str = std::any::type_name::<T>();
let tp_str = core::any::type_name::<T>();
parse_typed_custom_function_raw(input, tp_str)
}

Expand All @@ -381,14 +381,14 @@ pub fn parse_typed_custom_function_raw(

pub fn parse_sanitizer_kind<K>(input: ParseStream) -> syn::Result<(K, Ident)>
where
K: std::str::FromStr + kinded::Kind + std::fmt::Display + 'static,
K: core::str::FromStr + kinded::Kind + core::fmt::Display + 'static,
{
parse_kind("sanitizer", input)
}

pub fn parse_validator_kind<K>(input: ParseStream) -> syn::Result<(K, Ident)>
where
K: std::str::FromStr + kinded::Kind + std::fmt::Display + 'static,
K: core::str::FromStr + kinded::Kind + core::fmt::Display + 'static,
{
parse_kind("validator", input)
}
Expand All @@ -397,7 +397,7 @@ where
/// Build a helpful error on failure.
fn parse_kind<K>(attr_type: &str, input: ParseStream) -> syn::Result<(K, Ident)>
where
K: std::str::FromStr + kinded::Kind + std::fmt::Display + 'static,
K: core::str::FromStr + kinded::Kind + core::fmt::Display + 'static,
{
let ident: Ident = input.parse()?;
let attr_name = ident.to_string();
Expand Down
4 changes: 2 additions & 2 deletions nutype_macros/src/float/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::{
collections::HashSet,
use core::{
fmt::{Debug, Display},
marker::PhantomData,
str::FromStr,
};
use std::collections::HashSet;

use proc_macro2::TokenStream;
use quote::ToTokens;
Expand Down
2 changes: 1 addition & 1 deletion nutype_macros/src/float/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::common::models::{
#[kinded(display = "snake_case", derive(Hash))]
pub enum FloatSanitizer<T> {
With(TypedCustomFunction),
_Phantom(std::marker::PhantomData<T>),
_Phantom(core::marker::PhantomData<T>),
}

pub type SpannedFloatSanitizer<T> = SpannedItem<FloatSanitizer<T>>;
Expand Down
2 changes: 1 addition & 1 deletion nutype_macros/src/float/parse.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{
use core::{
fmt::{Debug, Display},
str::FromStr,
};
Expand Down
4 changes: 2 additions & 2 deletions nutype_macros/src/integer/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::{
collections::HashSet,
use core::{
fmt::{Debug, Display},
marker::PhantomData,
str::FromStr,
};
use std::collections::HashSet;

use proc_macro2::TokenStream;
use quote::ToTokens;
Expand Down
2 changes: 1 addition & 1 deletion nutype_macros/src/integer/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::common::models::{
#[kinded(display = "snake_case")]
pub enum IntegerSanitizer<T> {
With(TypedCustomFunction),
_Phantom(std::marker::PhantomData<T>),
_Phantom(core::marker::PhantomData<T>),
}

pub type SpannedIntegerSanitizer<T> = SpannedItem<IntegerSanitizer<T>>;
Expand Down
2 changes: 1 addition & 1 deletion nutype_macros/src/integer/parse.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{
use core::{
fmt::{Debug, Display},
str::FromStr,
};
Expand Down
12 changes: 6 additions & 6 deletions test_suite/src/test_helpers.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
pub mod traits {
pub fn should_implement_hash<T: std::hash::Hash>() {}
pub fn should_implement_debug<T: std::fmt::Debug>() {}
pub fn should_implement_from<T: std::convert::From<Inner>, Inner>() {}
pub fn should_implement_try_from<T: std::convert::TryFrom<Inner>, Inner>() {}
pub fn should_implement_from_str<T: std::str::FromStr>() {}
pub fn should_implement_borrow<T: std::borrow::Borrow<Borrowed>, Borrowed: ?Sized>() {}
pub fn should_implement_hash<T: core::hash::Hash>() {}
pub fn should_implement_debug<T: core::fmt::Debug>() {}
pub fn should_implement_from<T: core::convert::From<Inner>, Inner>() {}
pub fn should_implement_try_from<T: core::convert::TryFrom<Inner>, Inner>() {}
pub fn should_implement_from_str<T: core::str::FromStr>() {}
pub fn should_implement_borrow<T: core::borrow::Borrow<Borrowed>, Borrowed: ?Sized>() {}
pub fn should_implement_clone<T: Clone>() {}
pub fn should_implement_copy<T: Copy>() {}
pub fn should_implement_eq<T: Eq>() {}
Expand Down
27 changes: 14 additions & 13 deletions test_suite/tests/any.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use nutype::nutype;
use std::borrow::Cow;
use std::cmp::{Ordering, PartialEq, PartialOrd};
extern crate alloc;
use alloc::borrow::Cow;
use core::cmp::{Ordering, PartialEq, PartialOrd};
use std::collections::{HashMap, HashSet};
use std::fmt::Debug;
use std::hash::Hash;
use core::fmt::Debug;
use core::hash::Hash;
use test_suite::test_helpers::traits::*;

// Inner custom type, which is unknown to nutype
Expand All @@ -27,13 +28,13 @@ impl Point {
}
}

impl std::fmt::Display for Point {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl core::fmt::Display for Point {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{},{}", self.x, self.y)
}
}

impl std::str::FromStr for Point {
impl core::str::FromStr for Point {
type Err = &'static str;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Expand Down Expand Up @@ -96,7 +97,7 @@ mod traits {

#[test]
fn test_partial_ord() {
use std::cmp::Ordering;
use core::cmp::Ordering;

let loc1 = Location::new(Point::new(1, 1));
let loc2 = Location::new(Point::new(1, 2));
Expand All @@ -108,7 +109,7 @@ mod traits {

#[test]
fn test_ord() {
use std::cmp::Ordering;
use core::cmp::Ordering;

let loc1 = Location::new(Point::new(1, 1));
let loc2 = Location::new(Point::new(1, 2));
Expand Down Expand Up @@ -163,7 +164,7 @@ mod traits {

#[test]
fn test_borrow() {
use std::borrow::Borrow;
use core::borrow::Borrow;

let location = Location::from(Point::new(3, 4));
let point: &Point = location.borrow();
Expand Down Expand Up @@ -592,8 +593,8 @@ mod with_generics {
}

{
let nan1 = WrapperPartialOrd::new(std::f64::NAN);
let nan2 = WrapperPartialOrd::new(std::f64::NAN);
let nan1 = WrapperPartialOrd::new(f64::NAN);
let nan2 = WrapperPartialOrd::new(f64::NAN);
assert_eq!(nan1.partial_cmp(&nan2), None);
}
}
Expand Down Expand Up @@ -882,7 +883,7 @@ mod with_generics {

#[test]
fn test_derive_borrow_with_generic_boundaries_and_validation_and_sanitization() {
use std::borrow::Borrow;
use core::borrow::Borrow;

#[nutype(
sanitize(with = |mut v| { v.sort(); v }),
Expand Down
4 changes: 2 additions & 2 deletions test_suite/tests/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ mod traits {

#[test]
fn test_trait_borrow() {
use std::borrow::Borrow;
use core::borrow::Borrow;

#[nutype(derive(Borrow))]
pub struct Age(f64);
Expand Down Expand Up @@ -517,7 +517,7 @@ mod traits {
#[cfg(test)]
mod tests {
use super::*;
use std::cmp::Ordering;
use core::cmp::Ordering;

#[test]
fn test_trait_ord_f32() {
Expand Down
2 changes: 1 addition & 1 deletion test_suite/tests/integer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ mod traits {

#[test]
fn test_trait_borrow() {
use std::borrow::Borrow;
use core::borrow::Borrow;

#[nutype(derive(Borrow))]
pub struct Age(u8);
Expand Down
6 changes: 3 additions & 3 deletions test_suite/tests/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ mod validators {

#[test]
fn test_error() {
fn ensure_type_implements_error<T: std::error::Error>() {}
fn ensure_type_implements_error<T: core::error::Error>() {}

#[nutype(validate(not_empty), derive(Debug, PartialEq))]
pub struct Email(String);
Expand Down Expand Up @@ -391,7 +391,7 @@ mod derives {

#[test]
fn test_trait_borrow_str() {
use std::borrow::Borrow;
use core::borrow::Borrow;

#[nutype(derive(Borrow))]
pub struct Name(String);
Expand All @@ -403,7 +403,7 @@ mod derives {

#[test]
fn test_trait_borrow_string() {
use std::borrow::Borrow;
use core::borrow::Borrow;

#[nutype(derive(Borrow))]
pub struct Name(String);
Expand Down

0 comments on commit 87b51c5

Please sign in to comment.