Skip to content

Commit 5629f42

Browse files
authored
Merge pull request #58 from mina86/a
Make library no_std and introduce std and unstable features
2 parents a34f5f2 + 2b24610 commit 5629f42

File tree

9 files changed

+91
-81
lines changed

9 files changed

+91
-81
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,17 @@ project adheres to [Semantic Versioning](http://semver.org/).
99

1010
### Changed
1111

12+
- Introduce `std` (enabled by default) and `unstable` features ([#58]). They
13+
enable corresponding serde’s features and if either is enabled, `Error`
14+
implements `std::error::Error` trait. By itself, `serde-json-wasm` is now
15+
`no_std`; it’s up to serde’s features whether the entire build is. **Please
16+
note:** this potentially breaks `default-features = false` builds.
17+
1218
- Serialize / deserialize `u128`/`i128` types as numbers instead of strings
1319
([#59]).<br/> **Please note:** this breaks deserialization of `u128`/`i128`
1420
serialized with older versions of `serde-json-wasm`.
1521

22+
[#58]: https://github.com/CosmWasm/serde-json-wasm/pull/58
1623
[#59]: https://github.com/CosmWasm/serde-json-wasm/pull/59
1724

1825
## [0.5.1] - 2023-04-11

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,13 @@ exclude = [
2323
]
2424

2525
[dependencies]
26-
serde = { version = "^1.0.80", default-features = false, features = ["alloc"] }
26+
serde = { version = "^1.0.181", default-features = false, features = ["alloc"] }
2727

2828
[dev-dependencies]
2929
serde_derive = "^1.0.80"
3030
serde_json = "^1.0.99"
31+
32+
[features]
33+
default = ["std"]
34+
std = ["serde/std"]
35+
unstable = ["serde/unstable"]

src/de/errors.rs

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1+
use alloc::string::{String, ToString};
2+
13
use serde::de;
2-
use std::{error, fmt};
34

45
/// Deserialization result
56
pub type Result<T> = core::result::Result<T, Error>;
67

7-
/// This type represents all possible errors that can occur when deserializing JSON data
8+
/// This type represents all possible errors that can occur when deserializing
9+
/// JSON data
10+
///
11+
/// It implements [`std::error::Error`] trait so long as either `std` or
12+
/// `unstable` features are enabled. `std` is enabled by default and disabling
13+
/// it makes the crate `no_std`. `unstable` makes it necessary to build code
14+
/// with nightly compiler.
815
#[derive(Debug, PartialEq, Eq)]
916
#[non_exhaustive]
1017
pub enum Error {
@@ -72,27 +79,16 @@ pub enum Error {
7279
Custom(String),
7380
}
7481

75-
impl error::Error for Error {
76-
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
77-
None
78-
}
79-
80-
fn description(&self) -> &str {
81-
"(use display)"
82-
}
83-
}
82+
impl de::StdError for Error {}
8483

8584
impl de::Error for Error {
86-
fn custom<T>(msg: T) -> Self
87-
where
88-
T: fmt::Display,
89-
{
85+
fn custom<T: core::fmt::Display>(msg: T) -> Self {
9086
Error::Custom(msg.to_string())
9187
}
9288
}
9389

94-
impl fmt::Display for Error {
95-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
90+
impl core::fmt::Display for Error {
91+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
9692
write!(
9793
f,
9894
"{}",

src/de/mod.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ mod map;
66
mod seq;
77
mod unescape;
88

9+
use alloc::string::String;
10+
911
pub use errors::{Error, Result};
1012

1113
use serde::de::{self, Visitor};
1214

1315
use self::enum_::{StructVariantAccess, UnitVariantAccess};
1416
use self::map::MapAccess;
1517
use self::seq::SeqAccess;
16-
use std::str::from_utf8;
1718

1819
/// Deserializer will parse serde-json-wasm flavored JSON into a
1920
/// serde-annotated struct
@@ -126,7 +127,7 @@ impl<'a> Deserializer<'a> {
126127
)?))
127128
} else {
128129
Ok(StringLike::Borrowed(
129-
from_utf8(&self.slice[start..end])
130+
core::str::from_utf8(&self.slice[start..end])
130131
.map_err(|_| Error::InvalidUnicodeCodePoint)?,
131132
))
132133
};
@@ -651,6 +652,11 @@ where
651652
#[cfg(test)]
652653
mod tests {
653654
use super::from_str;
655+
656+
use alloc::string::{String, ToString};
657+
use alloc::vec;
658+
use alloc::vec::Vec;
659+
654660
use serde_derive::{Deserialize, Serialize};
655661

656662
#[derive(Debug, Deserialize, PartialEq)]
@@ -1103,7 +1109,7 @@ mod tests {
11031109

11041110
#[test]
11051111
fn numbered_key_maps() {
1106-
use std::collections::BTreeMap;
1112+
use alloc::collections::BTreeMap;
11071113

11081114
// u8
11091115
let mut ranking: BTreeMap<u8, String> = BTreeMap::new();

src/de/unescape.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use std::convert::TryFrom;
1+
use alloc::string::String;
2+
use alloc::vec::Vec;
3+
use core::convert::TryFrom;
24

35
use super::errors::{Error, Result};
46

src/lib.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@
5454
#![deny(missing_docs)]
5555
#![deny(rust_2018_compatibility)]
5656
#![deny(rust_2018_idioms)]
57+
// Note: Even though we declare the crate as `no_std`, by default `std` feature
58+
// is enabled which enables serde’s `std` feature which makes our dependency
59+
// non-`no_std`. This `no_std` declaration only makes sure that our code
60+
// doesn’t depend on `std` directly (except for tests).
61+
#![no_std]
62+
63+
extern crate alloc;
64+
#[cfg(test)]
65+
extern crate std;
5766

5867
pub mod de;
5968
pub mod ser;
@@ -65,7 +74,11 @@ pub use self::ser::{to_string, to_vec};
6574

6675
#[cfg(test)]
6776
mod test {
68-
use std::collections::BTreeMap;
77+
use alloc::borrow::ToOwned;
78+
use alloc::collections::BTreeMap;
79+
use alloc::string::{String, ToString};
80+
use alloc::vec;
81+
use alloc::vec::Vec;
6982

7083
use super::*;
7184
use serde_derive::{Deserialize, Serialize};
@@ -104,7 +117,7 @@ mod test {
104117
fn can_serde() {
105118
let min = Item {
106119
model: Model::Comment,
107-
title: "".to_string(),
120+
title: String::new(),
108121
content: None,
109122
list: vec![],
110123
published: false,
@@ -121,12 +134,12 @@ mod test {
121134
},
122135
title: "Nice message".to_string(),
123136
content: Some("Happy \"blogging\" 👏\n\n\tCheers, I'm out\0\0\0".to_string()),
124-
list: vec![0, 1, 2, 3, 42, 154841, std::u32::MAX],
137+
list: vec![0, 1, 2, 3, 42, 154841, u32::MAX],
125138
published: true,
126139
comments: vec![CommentId(2), CommentId(700)],
127140
stats: Stats {
128-
views: std::u64::MAX,
129-
score: std::i64::MIN,
141+
views: u64::MAX,
142+
score: i64::MIN,
130143
},
131144
balances,
132145
};

src/ser/map.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::fmt;
2-
31
use serde::{ser, Serialize};
42

53
use crate::ser::{Error, Result, Serializer};
@@ -55,7 +53,7 @@ struct MapKeySerializer<'a> {
5553
}
5654

5755
pub(crate) fn key_must_be_a_string() -> Error {
58-
Error::Custom("JSON object key is required to be a string type.".to_string())
56+
Error::Custom("JSON object key is required to be a string type.".into())
5957
}
6058

6159
macro_rules! serialize_unsigned_key {
@@ -252,7 +250,7 @@ impl<'a> ser::Serializer for MapKeySerializer<'a> {
252250

253251
fn collect_str<T>(self, _value: &T) -> Result<()>
254252
where
255-
T: ?Sized + fmt::Display,
253+
T: ?Sized + core::fmt::Display,
256254
{
257255
unreachable!()
258256
}

0 commit comments

Comments
 (0)