Added no_std support.

This commit is contained in:
Dion Dokter 2020-05-09 19:42:54 +02:00 committed by TheOddGarlic
parent 519df123a5
commit d44e382a9a
10 changed files with 102 additions and 43 deletions

View file

@ -20,15 +20,17 @@ edition = "2018"
members = ['test-suite'] members = ['test-suite']
[dependencies] [dependencies]
serde = "1.0.97" serde = { version = "1.0.97", default-features = false, features = ["alloc"] }
indexmap = { version = "1.0", optional = true } indexmap = { version = "1.0", optional = true }
hashbrown = { version = "0.7.2" }
[dev-dependencies] [dev-dependencies]
serde_derive = "1.0" serde_derive = "1.0"
serde_json = "1.0" serde_json = { version = "1.0", default-features = false, features = ["alloc"] }
[features] [features]
default = [] default = ["std"]
std = ["serde/std"]
# Use indexmap rather than BTreeMap as the map type of toml::Value. # Use indexmap rather than BTreeMap as the map type of toml::Value.
# This allows data to be read into a Value and written back to a TOML string # This allows data to be read into a Value and written back to a TOML string

View file

@ -1,6 +1,13 @@
#[cfg(feature = "std")]
use std::error; use std::error;
use std::fmt;
use std::str::{self, FromStr}; use core::fmt;
use core::str::{self, FromStr};
#[cfg(not(feature = "std"))]
use alloc::format;
#[cfg(not(feature = "std"))]
use alloc::string::ToString;
use serde::{de, ser}; use serde::{de, ser};
@ -541,4 +548,5 @@ impl fmt::Display for DatetimeParseError {
} }
} }
#[cfg(feature = "std")]
impl error::Error for DatetimeParseError {} impl error::Error for DatetimeParseError {}

View file

@ -4,15 +4,26 @@
//! into Rust structures. Note that some top-level functions here are also //! into Rust structures. Note that some top-level functions here are also
//! provided at the top of the crate. //! provided at the top of the crate.
use std::borrow::Cow; use alloc::borrow::Cow;
use std::collections::HashMap; #[cfg(not(feature = "std"))]
use alloc::borrow::ToOwned;
#[cfg(not(feature = "std"))]
use alloc::boxed::Box;
#[cfg(not(feature = "std"))]
use alloc::string::String;
#[cfg(not(feature = "std"))]
use alloc::string::ToString;
use alloc::vec;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use core::f64;
use core::fmt;
use core::iter;
use core::marker::PhantomData;
use core::str;
use hashbrown::HashMap;
#[cfg(feature = "std")]
use std::error; use std::error;
use std::f64;
use std::fmt;
use std::iter;
use std::marker::PhantomData;
use std::str;
use std::vec;
use serde::de; use serde::de;
use serde::de::value::BorrowedStrDeserializer; use serde::de::value::BorrowedStrDeserializer;
@ -2082,6 +2093,7 @@ impl Error {
} }
} }
#[cfg(feature = "std")]
impl std::convert::From<Error> for std::io::Error { impl std::convert::From<Error> for std::io::Error {
fn from(e: Error) -> Self { fn from(e: Error) -> Self {
std::io::Error::new(std::io::ErrorKind::InvalidData, e.to_string()) std::io::Error::new(std::io::ErrorKind::InvalidData, e.to_string())
@ -2171,6 +2183,7 @@ impl fmt::Display for Error {
} }
} }
#[cfg(feature = "std")]
impl error::Error for Error {} impl error::Error for Error {}
impl de::Error for Error { impl de::Error for Error {

View file

@ -152,6 +152,10 @@
// and lets them ensure that there is indeed no unsafe code as opposed to // and lets them ensure that there is indeed no unsafe code as opposed to
// something they couldn't detect (e.g. unsafe added via macro expansion, etc). // something they couldn't detect (e.g. unsafe added via macro expansion, etc).
#![forbid(unsafe_code)] #![forbid(unsafe_code)]
// When not testing and when the 'std' feature is not enabled, use no_std.
#![cfg_attr(all(not(test), not(feature = "std")), no_std)]
extern crate alloc;
pub mod map; pub mod map;
pub mod value; pub mod value;

View file

@ -428,6 +428,9 @@ pub fn push_toml(root: &mut Value, path: &[&str]) {
} }
fn traverse<'a>(root: &'a mut Value, path: &[&str]) -> &'a mut Value { fn traverse<'a>(root: &'a mut Value, path: &[&str]) -> &'a mut Value {
#[cfg(not(feature = "std"))]
use alloc::borrow::ToOwned;
let mut cur = root; let mut cur = root;
for &key in path { for &key in path {
// Lexical lifetimes :D // Lexical lifetimes :D

View file

@ -15,15 +15,18 @@
//! [`LinkedHashMap`]: https://docs.rs/linked-hash-map/*/linked_hash_map/struct.LinkedHashMap.html //! [`LinkedHashMap`]: https://docs.rs/linked-hash-map/*/linked_hash_map/struct.LinkedHashMap.html
use crate::value::Value; use crate::value::Value;
use core::borrow::Borrow;
use core::fmt::{self, Debug};
use core::hash::Hash;
use core::iter::FromIterator;
use core::ops;
use serde::{de, ser}; use serde::{de, ser};
use std::borrow::Borrow;
use std::fmt::{self, Debug}; #[cfg(not(feature = "std"))]
use std::hash::Hash; use alloc::string::String;
use std::iter::FromIterator;
use std::ops;
#[cfg(not(feature = "preserve_order"))] #[cfg(not(feature = "preserve_order"))]
use std::collections::{btree_map, BTreeMap}; use alloc::collections::{btree_map, BTreeMap};
#[cfg(feature = "preserve_order")] #[cfg(feature = "preserve_order")]
use indexmap::{self, IndexMap}; use indexmap::{self, IndexMap};
@ -144,10 +147,10 @@ impl Map<String, Value> {
where where
S: Into<String>, S: Into<String>,
{ {
#[cfg(not(feature = "preserve_order"))]
use alloc::collections::btree_map::Entry as EntryImpl;
#[cfg(feature = "preserve_order")] #[cfg(feature = "preserve_order")]
use indexmap::map::Entry as EntryImpl; use indexmap::map::Entry as EntryImpl;
#[cfg(not(feature = "preserve_order"))]
use std::collections::btree_map::Entry as EntryImpl;
match self.map.entry(key.into()) { match self.map.entry(key.into()) {
EntryImpl::Vacant(vacant) => Entry::Vacant(VacantEntry { vacant }), EntryImpl::Vacant(vacant) => Entry::Vacant(VacantEntry { vacant }),

View file

@ -25,11 +25,18 @@
//! # fn main() {} //! # fn main() {}
//! ``` //! ```
use std::cell::Cell; use alloc::rc::Rc;
#[cfg(not(feature = "std"))]
use alloc::string::String;
#[cfg(not(feature = "std"))]
use alloc::string::ToString;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use core::cell::Cell;
use core::fmt::{self, Write};
use core::marker;
#[cfg(feature = "std")]
use std::error; use std::error;
use std::fmt::{self, Write};
use std::marker;
use std::rc::Rc;
use crate::datetime; use crate::datetime;
use serde::ser; use serde::ser;
@ -1548,6 +1555,7 @@ impl fmt::Display for Error {
} }
} }
#[cfg(feature = "std")]
impl error::Error for Error {} impl error::Error for Error {}
impl ser::Error for Error { impl ser::Error for Error {

View file

@ -1,8 +1,11 @@
use core::borrow::Borrow;
use core::cmp::Ordering;
use core::fmt;
use core::hash::{Hash, Hasher};
use serde::{de, ser}; use serde::{de, ser};
use std::borrow::Borrow;
use std::cmp::Ordering; #[cfg(not(feature = "std"))]
use std::fmt; use alloc::string::String;
use std::hash::{Hash, Hasher};
pub(crate) const NAME: &str = "$__toml_private_Spanned"; pub(crate) const NAME: &str = "$__toml_private_Spanned";
pub(crate) const START: &str = "$__toml_private_start"; pub(crate) const START: &str = "$__toml_private_start";
@ -113,7 +116,7 @@ where
where where
D: de::Deserializer<'de>, D: de::Deserializer<'de>,
{ {
struct SpannedVisitor<T>(::std::marker::PhantomData<T>); struct SpannedVisitor<T>(::core::marker::PhantomData<T>);
impl<'de, T> de::Visitor<'de> for SpannedVisitor<T> impl<'de, T> de::Visitor<'de> for SpannedVisitor<T>
where where
@ -151,7 +154,7 @@ where
} }
} }
let visitor = SpannedVisitor(::std::marker::PhantomData); let visitor = SpannedVisitor(::core::marker::PhantomData);
static FIELDS: [&str; 3] = [START, END, VALUE]; static FIELDS: [&str; 3] = [START, END, VALUE];
deserializer.deserialize_struct(NAME, &FIELDS, visitor) deserializer.deserialize_struct(NAME, &FIELDS, visitor)

View file

@ -1,8 +1,8 @@
use std::borrow::Cow; use alloc::borrow::Cow;
use std::char; use alloc::string;
use std::str; use alloc::string::String as StdString;
use std::string; use core::char;
use std::string::String as StdString; use core::str;
use self::Token::*; use self::Token::*;
@ -507,6 +507,9 @@ impl MaybeString {
} }
fn to_owned(&mut self, input: &str) { fn to_owned(&mut self, input: &str) {
#[cfg(not(feature = "std"))]
use alloc::borrow::ToOwned;
match *self { match *self {
MaybeString::NotEscaped(start) => { MaybeString::NotEscaped(start) => {
*self = MaybeString::Owned(input[start..].to_owned()); *self = MaybeString::Owned(input[start..].to_owned());

View file

@ -1,12 +1,24 @@
//! Definition of a TOML value //! Definition of a TOML value
use std::collections::{BTreeMap, HashMap}; use alloc::collections::BTreeMap;
use std::fmt; use alloc::vec;
use std::hash::Hash; use core::fmt;
use std::mem::discriminant; use core::hash::Hash;
use std::ops; use core::mem::discriminant;
use std::str::FromStr; use core::ops;
use std::vec; use core::str::FromStr;
use hashbrown::HashMap;
#[cfg(not(feature = "std"))]
use alloc::borrow::ToOwned;
#[cfg(not(feature = "std"))]
use alloc::format;
#[cfg(not(feature = "std"))]
use alloc::string::String;
#[cfg(not(feature = "std"))]
use alloc::string::ToString;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use serde::de; use serde::de;
use serde::de::IntoDeserializer; use serde::de::IntoDeserializer;