From 5d2cbcdd4b146e98b5aa2200de7a8ae6231bf0ba Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Thu, 30 Dec 2021 19:55:10 -0800 Subject: [PATCH] Eliminate lib module --- src/de.rs | 10 ++++++++-- src/error.rs | 9 +++++++-- src/io/core.rs | 4 +++- src/lexical/bhcomp.rs | 2 +- src/lexical/bignum.rs | 2 +- src/lexical/math.rs | 3 ++- src/lexical/num.rs | 2 +- src/lexical/rounding.rs | 2 +- src/lexical/shift.rs | 2 +- src/lib.rs | 34 ++-------------------------------- src/map.rs | 17 +++++++++++++---- src/number.rs | 6 ++++-- src/raw.rs | 3 ++- src/read.rs | 7 +++++-- src/ser.rs | 6 ++++-- src/value/de.rs | 8 ++++++-- src/value/from.rs | 6 ++++-- src/value/index.rs | 9 ++++++--- src/value/mod.rs | 8 ++++++-- src/value/partial_eq.rs | 2 +- src/value/ser.rs | 6 +++++- tests/lexical.rs | 2 ++ 22 files changed, 85 insertions(+), 65 deletions(-) diff --git a/src/de.rs b/src/de.rs index a01c476..7eba7f4 100644 --- a/src/de.rs +++ b/src/de.rs @@ -3,10 +3,16 @@ use crate::error::{Error, ErrorCode, Result}; #[cfg(feature = "float_roundtrip")] use crate::lexical; -use crate::lib::str::FromStr; -use crate::lib::*; use crate::number::Number; use crate::read::{self, Fused, Reference}; +use alloc::string::String; +use alloc::vec::Vec; +#[cfg(feature = "float_roundtrip")] +use core::iter; +use core::iter::FusedIterator; +use core::marker::PhantomData; +use core::result; +use core::str::FromStr; use serde::de::{self, Expected, Unexpected}; use serde::{forward_to_deserialize_any, serde_if_integer128}; diff --git a/src/error.rs b/src/error.rs index 4219d32..6390c43 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,9 +1,14 @@ //! When serializing or deserializing JSON goes wrong. use crate::io; -use crate::lib::str::FromStr; -use crate::lib::*; +use alloc::boxed::Box; +use alloc::string::{String, ToString}; +use core::fmt::{self, Debug, Display}; +use core::result; +use core::str::FromStr; use serde::{de, ser}; +#[cfg(feature = "std")] +use std::error; /// This type represents all possible errors that can occur when serializing or /// deserializing JSON data. diff --git a/src/io/core.rs b/src/io/core.rs index 97354eb..465ab8b 100644 --- a/src/io/core.rs +++ b/src/io/core.rs @@ -1,7 +1,9 @@ //! Reimplements core logic and types from `std::io` in an `alloc`-friendly //! fashion. -use crate::lib::*; +use alloc::vec::Vec; +use core::fmt::{self, Display}; +use core::result; pub enum ErrorKind { Other, diff --git a/src/lexical/bhcomp.rs b/src/lexical/bhcomp.rs index 6d76c59..1f2a7bb 100644 --- a/src/lexical/bhcomp.rs +++ b/src/lexical/bhcomp.rs @@ -12,7 +12,7 @@ use super::float::*; use super::math::*; use super::num::*; use super::rounding::*; -use crate::lib::{cmp, mem}; +use core::{cmp, mem}; // MANTISSA diff --git a/src/lexical/bignum.rs b/src/lexical/bignum.rs index dee4e68..f9551f5 100644 --- a/src/lexical/bignum.rs +++ b/src/lexical/bignum.rs @@ -3,7 +3,7 @@ //! Big integer type definition. use super::math::*; -use crate::lib::Vec; +use alloc::vec::Vec; /// Storage for a big integer type. #[derive(Clone, PartialEq, Eq)] diff --git a/src/lexical/math.rs b/src/lexical/math.rs index 45fb00d..0925d0c 100644 --- a/src/lexical/math.rs +++ b/src/lexical/math.rs @@ -9,7 +9,8 @@ use super::large_powers; use super::num::*; use super::small_powers::*; -use crate::lib::{cmp, iter, mem, Vec}; +use alloc::vec::Vec; +use core::{cmp, iter, mem}; // ALIASES // ------- diff --git a/src/lexical/num.rs b/src/lexical/num.rs index 27c78ed..e47e003 100644 --- a/src/lexical/num.rs +++ b/src/lexical/num.rs @@ -2,7 +2,7 @@ //! Utilities for Rust numbers. -use crate::lib::ops; +use core::ops; /// Precalculated values of radix**i for i in range [0, arr.len()-1]. /// Each value can be **exactly** represented as that type. diff --git a/src/lexical/rounding.rs b/src/lexical/rounding.rs index d2704c9..6ec1292 100644 --- a/src/lexical/rounding.rs +++ b/src/lexical/rounding.rs @@ -5,7 +5,7 @@ use super::float::ExtendedFloat; use super::num::*; use super::shift::*; -use crate::lib::mem; +use core::mem; // MASKS diff --git a/src/lexical/shift.rs b/src/lexical/shift.rs index b0bd469..a0bae01 100644 --- a/src/lexical/shift.rs +++ b/src/lexical/shift.rs @@ -3,7 +3,7 @@ //! Bit-shift helpers. use super::float::ExtendedFloat; -use crate::lib::mem; +use core::mem; // Shift extended-precision float right `shift` bytes. #[inline] diff --git a/src/lib.rs b/src/lib.rs index c3a1f3d..9692ab6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -360,38 +360,8 @@ #![cfg_attr(not(feature = "std"), no_std)] #![cfg_attr(docsrs, feature(doc_cfg))] -//////////////////////////////////////////////////////////////////////////////// - extern crate alloc; -/// A facade around all the types we need from the `std`, `core`, and `alloc` -/// crates. This avoids elaborate import wrangling having to happen in every -/// module. -mod lib { - pub use core::cell::{Cell, RefCell}; - pub use core::clone::{self, Clone}; - pub use core::convert::{self, From, Into}; - pub use core::default::{self, Default}; - pub use core::fmt::{self, Debug, Display}; - pub use core::hash::{self, Hash, Hasher}; - pub use core::iter::FusedIterator; - pub use core::marker::{self, PhantomData}; - pub use core::ops::{Bound, RangeBounds}; - pub use core::result::{self, Result}; - pub use core::{borrow, char, cmp, iter, mem, num, ops, slice, str}; - - pub use alloc::borrow::{Cow, ToOwned}; - pub use alloc::boxed::Box; - pub use alloc::collections::{btree_map, BTreeMap}; - pub use alloc::string::{String, ToString}; - pub use alloc::vec::{self, Vec}; - - #[cfg(feature = "std")] - pub use std::error; -} - -//////////////////////////////////////////////////////////////////////////////// - #[cfg(feature = "std")] #[doc(inline)] pub use crate::de::from_reader; @@ -412,8 +382,8 @@ pub use crate::value::{from_value, to_value, Map, Number, Value}; macro_rules! tri { ($e:expr $(,)?) => { match $e { - crate::lib::Result::Ok(val) => val, - crate::lib::Result::Err(err) => return crate::lib::Result::Err(err), + core::result::Result::Ok(val) => val, + core::result::Result::Err(err) => return core::result::Result::Err(err), } }; } diff --git a/src/map.rs b/src/map.rs index 635aeb9..146eb6a 100644 --- a/src/map.rs +++ b/src/map.rs @@ -6,12 +6,19 @@ //! [`BTreeMap`]: https://doc.rust-lang.org/std/collections/struct.BTreeMap.html //! [`IndexMap`]: https://docs.rs/indexmap/*/indexmap/map/struct.IndexMap.html -use crate::lib::borrow::Borrow; -use crate::lib::iter::FromIterator; -use crate::lib::*; use crate::value::Value; +use alloc::string::String; +use core::borrow::Borrow; +use core::fmt::{self, Debug}; +use core::hash::Hash; +use core::iter::{FromIterator, FusedIterator}; +#[cfg(feature = "preserve_order")] +use core::mem; +use core::ops; use serde::de; +#[cfg(not(feature = "preserve_order"))] +use alloc::collections::{btree_map, BTreeMap}; #[cfg(feature = "preserve_order")] use indexmap::{self, IndexMap}; @@ -165,6 +172,8 @@ impl Map { no_btreemap_get_key_value, ))] { + use core::ops::{Bound, RangeBounds}; + struct Key<'a, Q: ?Sized>(&'a Q); impl<'a, Q: ?Sized> RangeBounds for Key<'a, Q> { @@ -202,7 +211,7 @@ impl Map { S: Into, { #[cfg(not(feature = "preserve_order"))] - use crate::lib::btree_map::Entry as EntryImpl; + use alloc::collections::btree_map::Entry as EntryImpl; #[cfg(feature = "preserve_order")] use indexmap::map::Entry as EntryImpl; diff --git a/src/number.rs b/src/number.rs index dfac753..b965271 100644 --- a/src/number.rs +++ b/src/number.rs @@ -1,6 +1,8 @@ use crate::de::ParserNumber; use crate::error::Error; -use crate::lib::*; +use core::fmt::{self, Debug, Display}; +#[cfg(not(feature = "arbitrary_precision"))] +use core::hash::{Hash, Hasher}; use serde::de::{self, Unexpected, Visitor}; use serde::{ forward_to_deserialize_any, serde_if_integer128, Deserialize, Deserializer, Serialize, @@ -286,7 +288,7 @@ impl Number { } } -impl fmt::Display for Number { +impl Display for Number { #[cfg(not(feature = "arbitrary_precision"))] fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { match self.n { diff --git a/src/raw.rs b/src/raw.rs index a0e1630..b853314 100644 --- a/src/raw.rs +++ b/src/raw.rs @@ -1,5 +1,6 @@ use crate::error::Error; -use crate::lib::*; +use core::fmt::{self, Debug, Display}; +use core::mem; use serde::de::value::BorrowedStrDeserializer; use serde::de::{ self, Deserialize, DeserializeSeed, Deserializer, IntoDeserializer, MapAccess, Unexpected, diff --git a/src/read.rs b/src/read.rs index 7bad570..c6186c2 100644 --- a/src/read.rs +++ b/src/read.rs @@ -1,6 +1,9 @@ use crate::error::{Error, ErrorCode, Result}; -use crate::lib::ops::Deref; -use crate::lib::*; +use alloc::vec::Vec; +use core::char; +use core::cmp; +use core::ops::Deref; +use core::str; #[cfg(feature = "std")] use crate::io; diff --git a/src/ser.rs b/src/ser.rs index 6637981..b497c56 100644 --- a/src/ser.rs +++ b/src/ser.rs @@ -2,8 +2,10 @@ use crate::error::{Error, ErrorCode, Result}; use crate::io; -use crate::lib::num::FpCategory; -use crate::lib::*; +use alloc::string::{String, ToString}; +use alloc::vec::Vec; +use core::fmt::{self, Display}; +use core::num::FpCategory; use serde::ser::{self, Impossible, Serialize}; use serde::serde_if_integer128; diff --git a/src/value/de.rs b/src/value/de.rs index 24ca826..347dbee 100644 --- a/src/value/de.rs +++ b/src/value/de.rs @@ -1,9 +1,13 @@ use crate::error::Error; -use crate::lib::str::FromStr; -use crate::lib::*; use crate::map::Map; use crate::number::Number; use crate::value::Value; +use alloc::borrow::{Cow, ToOwned}; +use alloc::string::String; +use alloc::vec::{self, Vec}; +use core::fmt; +use core::slice; +use core::str::FromStr; use serde::de::{ self, Deserialize, DeserializeSeed, EnumAccess, Expected, IntoDeserializer, MapAccess, SeqAccess, Unexpected, VariantAccess, Visitor, diff --git a/src/value/from.rs b/src/value/from.rs index 59e09fd..7b37ef6 100644 --- a/src/value/from.rs +++ b/src/value/from.rs @@ -1,8 +1,10 @@ use super::Value; -use crate::lib::iter::FromIterator; -use crate::lib::*; use crate::map::Map; use crate::number::Number; +use alloc::borrow::Cow; +use alloc::string::{String, ToString}; +use alloc::vec::Vec; +use core::iter::FromIterator; #[cfg(feature = "arbitrary_precision")] use serde::serde_if_integer128; diff --git a/src/value/index.rs b/src/value/index.rs index d759a1d..0d90a5d 100644 --- a/src/value/index.rs +++ b/src/value/index.rs @@ -1,6 +1,9 @@ use super::Value; -use crate::lib::*; use crate::map::Map; +use alloc::borrow::ToOwned; +use alloc::string::String; +use core::fmt::{self, Display}; +use core::ops; /// A type that can be used to index into a `serde_json::Value`. /// @@ -133,14 +136,14 @@ mod private { pub trait Sealed {} impl Sealed for usize {} impl Sealed for str {} - impl Sealed for super::String {} + impl Sealed for alloc::string::String {} impl<'a, T> Sealed for &'a T where T: ?Sized + Sealed {} } /// Used in panic messages. struct Type<'a>(&'a Value); -impl<'a> fmt::Display for Type<'a> { +impl<'a> Display for Type<'a> { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { match *self.0 { Value::Null => formatter.write_str("null"), diff --git a/src/value/mod.rs b/src/value/mod.rs index a28da66..3f00c95 100644 --- a/src/value/mod.rs +++ b/src/value/mod.rs @@ -92,7 +92,11 @@ use crate::error::Error; use crate::io; -use crate::lib::*; +use alloc::string::String; +use alloc::vec::Vec; +use core::fmt::{self, Debug, Display}; +use core::mem; +use core::str; use serde::de::DeserializeOwned; use serde::ser::Serialize; @@ -191,7 +195,7 @@ impl Debug for Value { } } -impl fmt::Display for Value { +impl Display for Value { /// Display a JSON value as a string. /// /// ``` diff --git a/src/value/partial_eq.rs b/src/value/partial_eq.rs index 354ea5a..b4ef84c 100644 --- a/src/value/partial_eq.rs +++ b/src/value/partial_eq.rs @@ -1,5 +1,5 @@ use super::Value; -use crate::lib::*; +use alloc::string::String; fn eq_i64(value: &Value, other: i64) -> bool { value.as_i64().map_or(false, |i| i == other) diff --git a/src/value/ser.rs b/src/value/ser.rs index 03cb12b..5f59c63 100644 --- a/src/value/ser.rs +++ b/src/value/ser.rs @@ -1,8 +1,12 @@ use crate::error::{Error, ErrorCode, Result}; -use crate::lib::*; use crate::map::Map; use crate::number::Number; use crate::value::{to_value, Value}; +use alloc::borrow::ToOwned; +use alloc::string::{String, ToString}; +use alloc::vec::Vec; +use core::fmt::Display; +use core::result; use serde::ser::{Impossible, Serialize}; #[cfg(feature = "arbitrary_precision")] diff --git a/tests/lexical.rs b/tests/lexical.rs index 928ad65..6e0f07b 100644 --- a/tests/lexical.rs +++ b/tests/lexical.rs @@ -20,6 +20,8 @@ clippy::wildcard_imports )] +extern crate alloc; + #[path = "../src/lexical/mod.rs"] mod lexical;