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

Fix warnings #705

Merged
merged 2 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 1 addition & 2 deletions serde_with/src/de/duplicates.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use super::impls::{foreach_map, foreach_set};
use super::impls::macros::{foreach_map, foreach_set};
use crate::{
duplicate_key_impls::{
DuplicateInsertsFirstWinsMap, DuplicateInsertsLastWinsSet, PreventDuplicateInsertsMap,
PreventDuplicateInsertsSet,
},
prelude::*,
MapFirstKeyWins, MapPreventDuplicates, SetLastValueWins, SetPreventDuplicates,
};
#[cfg(feature = "hashbrown_0_14")]
use hashbrown_0_14::{HashMap as HashbrownMap014, HashSet as HashbrownSet014};
Expand Down
171 changes: 90 additions & 81 deletions serde_with/src/de/impls.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub(crate) use self::macros::*;
use crate::{formats::*, prelude::*};
#[cfg(feature = "hashbrown_0_14")]
use hashbrown_0_14::{HashMap as HashbrownMap014, HashSet as HashbrownSet014};
Expand All @@ -12,91 +13,99 @@ use indexmap_2::{IndexMap as IndexMap2, IndexSet as IndexSet2};
#[cfg(feature = "alloc")]
type BoxedSlice<T> = Box<[T]>;

macro_rules! foreach_map {
($m:ident) => {
#[cfg(feature = "alloc")]
$m!(BTreeMap<K: Ord, V>, (|_size| BTreeMap::new()));
#[cfg(feature = "std")]
$m!(
HashMap<K: Eq + Hash, V, S: BuildHasher + Default>,
(|size| HashMap::with_capacity_and_hasher(size, Default::default()))
);
#[cfg(feature = "hashbrown_0_14")]
$m!(
HashbrownMap014<K: Eq + Hash, V, S: BuildHasher + Default>,
(|size| HashbrownMap014::with_capacity_and_hasher(size, Default::default()))
);
#[cfg(feature = "indexmap_1")]
$m!(
IndexMap<K: Eq + Hash, V, S: BuildHasher + Default>,
(|size| IndexMap::with_capacity_and_hasher(size, Default::default()))
);
#[cfg(feature = "indexmap_2")]
$m!(
IndexMap2<K: Eq + Hash, V, S: BuildHasher + Default>,
(|size| IndexMap2::with_capacity_and_hasher(size, Default::default()))
);
};
}
pub(crate) use foreach_map;
pub(crate) mod macros {
// The unused_import lint has false-positives around macros
// https://github.com/rust-lang/rust/issues/78894
#![allow(unused_import)]

macro_rules! foreach_set {
($m:ident) => {
#[cfg(feature = "alloc")]
$m!(BTreeSet<T: Ord>, (|_| BTreeSet::new()), insert);
#[cfg(feature = "std")]
$m!(
HashSet<T: Eq + Hash, S: BuildHasher + Default>,
(|size| HashSet::with_capacity_and_hasher(size, S::default())),
insert
);
#[cfg(feature = "hashbrown_0_14")]
$m!(
HashbrownSet014<T: Eq + Hash, S: BuildHasher + Default>,
(|size| HashbrownSet014::with_capacity_and_hasher(size, S::default())),
insert
);
#[cfg(feature = "indexmap_1")]
$m!(
IndexSet<T: Eq + Hash, S: BuildHasher + Default>,
(|size| IndexSet::with_capacity_and_hasher(size, S::default())),
insert
);
#[cfg(feature = "indexmap_2")]
$m!(
IndexSet2<T: Eq + Hash, S: BuildHasher + Default>,
(|size| IndexSet2::with_capacity_and_hasher(size, S::default())),
insert
);
};
}
pub(crate) use foreach_set;
macro_rules! foreach_map {
($m:ident) => {
#[cfg(feature = "alloc")]
$m!(BTreeMap<K: Ord, V>, (|_size| BTreeMap::new()));
#[cfg(feature = "std")]
$m!(
HashMap<K: Eq + Hash, V, S: BuildHasher + Default>,
(|size| HashMap::with_capacity_and_hasher(size, Default::default()))
);
#[cfg(feature = "hashbrown_0_14")]
$m!(
HashbrownMap014<K: Eq + Hash, V, S: BuildHasher + Default>,
(|size| HashbrownMap014::with_capacity_and_hasher(size, Default::default()))
);
#[cfg(feature = "indexmap_1")]
$m!(
IndexMap<K: Eq + Hash, V, S: BuildHasher + Default>,
(|size| IndexMap::with_capacity_and_hasher(size, Default::default()))
);
#[cfg(feature = "indexmap_2")]
$m!(
IndexMap2<K: Eq + Hash, V, S: BuildHasher + Default>,
(|size| IndexMap2::with_capacity_and_hasher(size, Default::default()))
);
};
}

macro_rules! foreach_seq {
($m:ident) => {
foreach_set!($m);
macro_rules! foreach_set {
($m:ident) => {
#[cfg(feature = "alloc")]
$m!(BTreeSet<T: Ord>, (|_| BTreeSet::new()), insert);
#[cfg(feature = "std")]
$m!(
HashSet<T: Eq + Hash, S: BuildHasher + Default>,
(|size| HashSet::with_capacity_and_hasher(size, S::default())),
insert
);
#[cfg(feature = "hashbrown_0_14")]
$m!(
HashbrownSet014<T: Eq + Hash, S: BuildHasher + Default>,
(|size| HashbrownSet014::with_capacity_and_hasher(size, S::default())),
insert
);
#[cfg(feature = "indexmap_1")]
$m!(
IndexSet<T: Eq + Hash, S: BuildHasher + Default>,
(|size| IndexSet::with_capacity_and_hasher(size, S::default())),
insert
);
#[cfg(feature = "indexmap_2")]
$m!(
IndexSet2<T: Eq + Hash, S: BuildHasher + Default>,
(|size| IndexSet2::with_capacity_and_hasher(size, S::default())),
insert
);
};
}

#[cfg(feature = "alloc")]
$m!(
BinaryHeap<T: Ord>,
(|size| BinaryHeap::with_capacity(size)),
push
);
#[cfg(feature = "alloc")]
$m!(BoxedSlice<T>, (|size| Vec::with_capacity(size)), push);
#[cfg(feature = "alloc")]
$m!(LinkedList<T>, (|_| LinkedList::new()), push_back);
#[cfg(feature = "alloc")]
$m!(Vec<T>, (|size| Vec::with_capacity(size)), push);
#[cfg(feature = "alloc")]
$m!(
VecDeque<T>,
(|size| VecDeque::with_capacity(size)),
push_back
);
};
macro_rules! foreach_seq {
($m:ident) => {
foreach_set!($m);

#[cfg(feature = "alloc")]
$m!(
BinaryHeap<T: Ord>,
(|size| BinaryHeap::with_capacity(size)),
push
);
#[cfg(feature = "alloc")]
$m!(BoxedSlice<T>, (|size| Vec::with_capacity(size)), push);
#[cfg(feature = "alloc")]
$m!(LinkedList<T>, (|_| LinkedList::new()), push_back);
#[cfg(feature = "alloc")]
$m!(Vec<T>, (|size| Vec::with_capacity(size)), push);
#[cfg(feature = "alloc")]
$m!(
VecDeque<T>,
(|size| VecDeque::with_capacity(size)),
push_back
);
};
}

// Make the macros available to the rest of the crate
pub(crate) use foreach_map;
pub(crate) use foreach_seq;
pub(crate) use foreach_set;
}
pub(crate) use foreach_seq;

///////////////////////////////////////////////////////////////////////////////
// region: Simple Wrapper types (e.g., Box, Option)
Expand Down
1 change: 0 additions & 1 deletion serde_with/src/schemars_0_8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use ::schemars_0_8::{
},
JsonSchema,
};
use std::borrow::Cow;

//===================================================================
// Trait Definition
Expand Down
6 changes: 2 additions & 4 deletions serde_with/src/ser/duplicates.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use super::impls::{foreach_map, foreach_set};
use crate::{
prelude::*, MapFirstKeyWins, MapPreventDuplicates, SetLastValueWins, SetPreventDuplicates,
};
use super::impls::macros::{foreach_map, foreach_set};
use crate::prelude::*;
#[cfg(feature = "hashbrown_0_14")]
use hashbrown_0_14::{HashMap as HashbrownMap014, HashSet as HashbrownSet014};
#[cfg(feature = "indexmap_1")]
Expand Down
58 changes: 34 additions & 24 deletions serde_with/src/ser/impls.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{formats, formats::Strictness, prelude::*};
pub(crate) use self::macros::*;
use crate::{formats::Strictness, prelude::*};
#[cfg(feature = "hashbrown_0_14")]
use hashbrown_0_14::{HashMap as HashbrownMap014, HashSet as HashbrownSet014};
#[cfg(feature = "indexmap_1")]
Expand All @@ -13,7 +14,12 @@ use indexmap_2::{IndexMap as IndexMap2, IndexSet as IndexSet2};
type BoxedSlice<T> = Box<[T]>;
type Slice<T> = [T];

macro_rules! foreach_map {
pub(crate) mod macros {
// The unused_import lint has false-positives around macros
// https://github.com/rust-lang/rust/issues/78894
#![allow(unused_import)]

macro_rules! foreach_map {
($m:ident) => {
#[cfg(feature = "alloc")]
$m!(BTreeMap<K, V>);
Expand All @@ -27,9 +33,8 @@ macro_rules! foreach_map {
$m!(IndexMap2<K, V, H: Sized>);
};
}
pub(crate) use foreach_map;

macro_rules! foreach_set {
macro_rules! foreach_set {
($m:ident, $T:tt) => {
#[cfg(feature = "alloc")]
$m!(BTreeSet<$T>);
Expand All @@ -46,30 +51,35 @@ macro_rules! foreach_set {
foreach_set!($m, T);
};
}
pub(crate) use foreach_set;

macro_rules! foreach_seq {
($m:ident, $T:tt) => {
foreach_set!($m, $T);
macro_rules! foreach_seq {
($m:ident, $T:tt) => {
foreach_set!($m, $T);

$m!(Slice<$T>);
$m!(Slice<$T>);

#[cfg(feature = "alloc")]
$m!(BinaryHeap<$T>);
#[cfg(feature = "alloc")]
$m!(BoxedSlice<$T>);
#[cfg(feature = "alloc")]
$m!(LinkedList<$T>);
#[cfg(feature = "alloc")]
$m!(Vec<$T>);
#[cfg(feature = "alloc")]
$m!(VecDeque<$T>);
};
($m:ident) => {
foreach_seq!($m, T);
};
#[cfg(feature = "alloc")]
$m!(BinaryHeap<$T>);
#[cfg(feature = "alloc")]
$m!(BoxedSlice<$T>);
#[cfg(feature = "alloc")]
$m!(LinkedList<$T>);
#[cfg(feature = "alloc")]
$m!(Vec<$T>);
#[cfg(feature = "alloc")]
$m!(VecDeque<$T>);
};
($m:
ident) => {
foreach_seq!($m, T);
};
}

// Make the macros available to the rest of the crate
pub(crate) use foreach_map;
pub(crate) use foreach_seq;
pub(crate) use foreach_set;
}
pub(crate) use foreach_seq;

///////////////////////////////////////////////////////////////////////////////
// region: Simple Wrapper types (e.g., Box, Option)
Expand Down
2 changes: 1 addition & 1 deletion serde_with/tests/chrono_0_4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::utils::{
};
use alloc::collections::BTreeMap;
use chrono_0_4::{DateTime, Duration, Local, NaiveDateTime, TimeZone, Utc};
use core::{iter::FromIterator, str::FromStr};
use core::str::FromStr;
use expect_test::expect;
use serde::{Deserialize, Serialize};
use serde_with::{
Expand Down
1 change: 0 additions & 1 deletion serde_with/tests/hashbrown_0_14.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
mod utils;

use crate::utils::{check_deserialization, check_error_deserialization, is_equal};
use core::iter::FromIterator;
use expect_test::expect;
use serde::{Deserialize, Serialize};
use serde_with::{serde_as, DisplayFromStr, Same};
Expand Down
1 change: 0 additions & 1 deletion serde_with/tests/indexmap_1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
mod utils;

use crate::utils::{check_deserialization, check_error_deserialization, is_equal};
use core::iter::FromIterator;
use expect_test::expect;
use indexmap_1::{IndexMap, IndexSet};
use serde::{Deserialize, Serialize};
Expand Down
1 change: 0 additions & 1 deletion serde_with/tests/indexmap_2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
mod utils;

use crate::utils::{check_deserialization, check_error_deserialization, is_equal};
use core::iter::FromIterator;
use expect_test::expect;
use indexmap_2::{IndexMap, IndexSet};
use serde::{Deserialize, Serialize};
Expand Down
3 changes: 1 addition & 2 deletions serde_with/tests/schemars_0_8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ fn schemars_custom_with() {

mod test_std {
use super::*;
use std::collections::{BTreeMap, BTreeSet, VecDeque};
use std::collections::{BTreeMap, VecDeque};

declare_snapshot_test! {
option {
Expand Down Expand Up @@ -178,7 +178,6 @@ mod test_std {
mod snapshots {
use super::*;
use serde_with::formats::*;
use std::collections::BTreeSet;

#[allow(dead_code)]
#[derive(JsonSchema, Serialize)]
Expand Down
1 change: 0 additions & 1 deletion serde_with/tests/serde_as/frominto.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use super::*;
use core::convert::TryFrom;
use serde_with::{FromInto, TryFromInto};

#[derive(Clone, Debug, PartialEq)]
Expand Down
1 change: 0 additions & 1 deletion serde_with/tests/serde_as/fromintoref.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use super::*;
use core::convert::TryFrom;
use serde_with::{FromIntoRef, TryFromIntoRef};

#[derive(Debug, PartialEq)]
Expand Down
5 changes: 1 addition & 4 deletions serde_with/tests/serde_as/pickfirst.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use super::*;
use serde_with::{
formats::{CommaSeparator, SpaceSeparator},
PickFirst, StringWithSeparator,
};
use serde_with::{formats::SpaceSeparator, PickFirst};

#[test]
fn test_pick_first_two() {
Expand Down
1 change: 0 additions & 1 deletion serde_with/tests/with_prefix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ mod utils;

use crate::utils::is_equal;
use alloc::collections::BTreeMap;
use core::iter::FromIterator;
use expect_test::expect;
use serde::{Deserialize, Serialize};
use serde_with::with_prefix;
Expand Down
1 change: 0 additions & 1 deletion serde_with_macros/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use core::iter::Iterator;
use darling::FromDeriveInput;
use proc_macro::TokenStream;
use proc_macro2::{TokenStream as TokenStream2, TokenTree};
Expand Down
Loading