Skip to content

Commit b11525b

Browse files
committed
Make the library no_std
Eliminate all uses of std crate from non-test code. For the most part it is a matter of using core or alloc namespaces instead. The only exception is std::error::Error trait. However, it is re-exported by serde as serde::{de,ser}::StdError so implement that instead. Tests use HashMap which requires std crate. Since test code doesn’t matter for no_std support, enable std crate when building tests. no_std support was tested by building for thumbv6m-none-eabi target. Issue: CosmWasm/cosmwasm#1484 Issue: CosmWasm#40
1 parent 90f684a commit b11525b

File tree

6 files changed

+181
-210
lines changed

6 files changed

+181
-210
lines changed

src/de/errors.rs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
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>;
@@ -72,27 +73,16 @@ pub enum Error {
7273
Custom(String),
7374
}
7475

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-
}
76+
impl serde::de::StdError for Error {}
8477

8578
impl de::Error for Error {
86-
fn custom<T>(msg: T) -> Self
87-
where
88-
T: fmt::Display,
89-
{
79+
fn custom<T: core::fmt::Display>(msg: T) -> Self {
9080
Error::Custom(msg.to_string())
9181
}
9282
}
9383

94-
impl fmt::Display for Error {
95-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
84+
impl core::fmt::Display for Error {
85+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
9686
write!(
9787
f,
9888
"{}",

src/de/mod.rs

Lines changed: 48 additions & 49 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
};
@@ -687,6 +688,11 @@ where
687688
#[cfg(test)]
688689
mod tests {
689690
use super::from_str;
691+
692+
use alloc::string::{String, ToString};
693+
use alloc::vec;
694+
use alloc::vec::Vec;
695+
690696
use serde_derive::{Deserialize, Serialize};
691697

692698
#[derive(Debug, Deserialize, PartialEq)]
@@ -947,7 +953,7 @@ mod tests {
947953
assert_eq!(
948954
from_str(r#"{ "description": "An ambient temperature sensor" }"#),
949955
Ok(Property {
950-
description: Some("An ambient temperature sensor".to_string()),
956+
description: Some("An ambient temperature sensor".into()),
951957
})
952958
);
953959

@@ -1036,7 +1042,7 @@ mod tests {
10361042
}
10371043

10381044
let expected = Users {
1039-
users: vec!["joe".to_string(), "alice".to_string()],
1045+
users: vec!["joe".into(), "alice".into()],
10401046
pagination: Pagination {
10411047
offset: 100,
10421048
limit: 20,
@@ -1116,7 +1122,7 @@ mod tests {
11161122
}
11171123

11181124
let element: Address = from_str(r#""johnny""#).unwrap();
1119-
assert_eq!(element, Address("johnny".to_string()));
1125+
assert_eq!(element, Address("johnny".into()));
11201126

11211127
let element: CommentId = from_str(r#"5464813"#).unwrap();
11221128
assert_eq!(element, CommentId(5464813));
@@ -1125,101 +1131,101 @@ mod tests {
11251131
assert_eq!(
11261132
element,
11271133
NewtypeDemo {
1128-
address: Address("johnny".to_string()),
1134+
address: Address("johnny".into()),
11291135
comment: CommentId(9897),
11301136
}
11311137
);
11321138
}
11331139

11341140
#[test]
11351141
fn numbered_key_maps() {
1136-
use std::collections::BTreeMap;
1142+
use alloc::collections::BTreeMap;
11371143

11381144
// u8
11391145
let mut ranking: BTreeMap<u8, String> = BTreeMap::new();
1140-
ranking.insert(1, "Elon".to_string());
1141-
ranking.insert(2, "Bazos".to_string());
1146+
ranking.insert(1, "Elon".into());
1147+
ranking.insert(2, "Bazos".into());
11421148
assert_eq!(
11431149
from_str::<BTreeMap<u8, String>>(r#"{"1": "Elon", "2": "Bazos"}"#).unwrap(),
11441150
ranking
11451151
);
11461152

11471153
// u16
11481154
let mut ranking: BTreeMap<u16, String> = BTreeMap::new();
1149-
ranking.insert(1, "Elon".to_string());
1150-
ranking.insert(2, "Bazos".to_string());
1155+
ranking.insert(1, "Elon".into());
1156+
ranking.insert(2, "Bazos".into());
11511157
assert_eq!(
11521158
from_str::<BTreeMap<u16, String>>(r#"{"1": "Elon", "2": "Bazos"}"#).unwrap(),
11531159
ranking
11541160
);
11551161

11561162
// u32
11571163
let mut ranking: BTreeMap<u32, String> = BTreeMap::new();
1158-
ranking.insert(1, "Elon".to_string());
1159-
ranking.insert(2, "Bazos".to_string());
1164+
ranking.insert(1, "Elon".into());
1165+
ranking.insert(2, "Bazos".into());
11601166
assert_eq!(
11611167
from_str::<BTreeMap<u32, String>>(r#"{"1": "Elon", "2": "Bazos"}"#).unwrap(),
11621168
ranking
11631169
);
11641170

11651171
// u64
11661172
let mut ranking: BTreeMap<u64, String> = BTreeMap::new();
1167-
ranking.insert(1, "Elon".to_string());
1168-
ranking.insert(2, "Bazos".to_string());
1173+
ranking.insert(1, "Elon".into());
1174+
ranking.insert(2, "Bazos".into());
11691175
assert_eq!(
11701176
from_str::<BTreeMap<u64, String>>(r#"{"1": "Elon", "2": "Bazos"}"#).unwrap(),
11711177
ranking
11721178
);
11731179

11741180
// u128
11751181
let mut ranking: BTreeMap<u128, String> = BTreeMap::new();
1176-
ranking.insert(1, "Elon".to_string());
1177-
ranking.insert(2, "Bazos".to_string());
1182+
ranking.insert(1, "Elon".into());
1183+
ranking.insert(2, "Bazos".into());
11781184
assert_eq!(
11791185
from_str::<BTreeMap<u128, String>>(r#"{"1": "Elon", "2": "Bazos"}"#).unwrap(),
11801186
ranking
11811187
);
11821188

11831189
// i8
11841190
let mut ranking: BTreeMap<i8, String> = BTreeMap::new();
1185-
ranking.insert(1, "Elon".to_string());
1186-
ranking.insert(2, "Bazos".to_string());
1191+
ranking.insert(1, "Elon".into());
1192+
ranking.insert(2, "Bazos".into());
11871193
assert_eq!(
11881194
from_str::<BTreeMap<i8, String>>(r#"{"1": "Elon", "2": "Bazos"}"#).unwrap(),
11891195
ranking
11901196
);
11911197

11921198
// i16
11931199
let mut ranking: BTreeMap<i16, String> = BTreeMap::new();
1194-
ranking.insert(1, "Elon".to_string());
1195-
ranking.insert(2, "Bazos".to_string());
1200+
ranking.insert(1, "Elon".into());
1201+
ranking.insert(2, "Bazos".into());
11961202
assert_eq!(
11971203
from_str::<BTreeMap<i16, String>>(r#"{"1": "Elon", "2": "Bazos"}"#).unwrap(),
11981204
ranking
11991205
);
12001206

12011207
// i32
12021208
let mut ranking: BTreeMap<i32, String> = BTreeMap::new();
1203-
ranking.insert(1, "Elon".to_string());
1204-
ranking.insert(2, "Bazos".to_string());
1209+
ranking.insert(1, "Elon".into());
1210+
ranking.insert(2, "Bazos".into());
12051211
assert_eq!(
12061212
from_str::<BTreeMap<i32, String>>(r#"{"1": "Elon", "2": "Bazos"}"#).unwrap(),
12071213
ranking
12081214
);
12091215

12101216
// i64
12111217
let mut ranking: BTreeMap<i64, String> = BTreeMap::new();
1212-
ranking.insert(1, "Elon".to_string());
1213-
ranking.insert(2, "Bazos".to_string());
1218+
ranking.insert(1, "Elon".into());
1219+
ranking.insert(2, "Bazos".into());
12141220
assert_eq!(
12151221
from_str::<BTreeMap<i64, String>>(r#"{"1": "Elon", "2": "Bazos"}"#).unwrap(),
12161222
ranking
12171223
);
12181224

12191225
// i128
12201226
let mut ranking: BTreeMap<i128, String> = BTreeMap::new();
1221-
ranking.insert(1, "Elon".to_string());
1222-
ranking.insert(2, "Bazos".to_string());
1227+
ranking.insert(1, "Elon".into());
1228+
ranking.insert(2, "Bazos".into());
12231229
assert_eq!(
12241230
from_str::<BTreeMap<i128, String>>(r#"{"1": "Elon", "2": "Bazos"}"#).unwrap(),
12251231
ranking
@@ -1250,12 +1256,7 @@ mod tests {
12501256
}"#,
12511257
)
12521258
.expect("simple");
1253-
assert_eq!(
1254-
m,
1255-
Msg {
1256-
name: "one".to_string()
1257-
}
1258-
);
1259+
assert_eq!(m, Msg { name: "one".into() });
12591260

12601261
let o: OptIn = from_str(
12611262
r#"{
@@ -1266,7 +1267,7 @@ mod tests {
12661267
assert_eq!(
12671268
o,
12681269
OptIn {
1269-
name: Some("two".to_string())
1270+
name: Some("two".into())
12701271
}
12711272
);
12721273

@@ -1280,10 +1281,8 @@ mod tests {
12801281
assert_eq!(
12811282
res,
12821283
Response {
1283-
log: Some("my log".to_string()),
1284-
messages: vec![Msg {
1285-
name: "one".to_string()
1286-
}],
1284+
log: Some("my log".into()),
1285+
messages: vec![Msg { name: "one".into() }],
12871286
}
12881287
);
12891288

@@ -1333,10 +1332,10 @@ mod tests {
13331332
assert_eq!(
13341333
res,
13351334
MyResult::Ok(Response {
1336-
log: Some("hello".to_string()),
1335+
log: Some("hello".into()),
13371336
messages: vec![Msg {
1338-
name: "fred".to_string(),
1339-
amount: Some("15".to_string())
1337+
name: "fred".into(),
1338+
amount: Some("15".into())
13401339
}]
13411340
})
13421341
);
@@ -1353,7 +1352,7 @@ mod tests {
13531352
assert_eq!(
13541353
res,
13551354
MyResult::Ok(Response {
1356-
log: Some("hello".to_string()),
1355+
log: Some("hello".into()),
13571356
messages: Vec::new()
13581357
})
13591358
);
@@ -1433,21 +1432,21 @@ mod tests {
14331432
properties: Properties {
14341433
temperature: Property {
14351434
ty: Type::Number,
1436-
unit: Some("celsius".to_string()),
1437-
description: Some("An ambient temperature sensor".to_string()),
1438-
href: "/properties/temperature".to_string(),
1435+
unit: Some("celsius".into()),
1436+
description: Some("An ambient temperature sensor".into()),
1437+
href: "/properties/temperature".into(),
14391438
},
14401439
humidity: Property {
14411440
ty: Type::Number,
1442-
unit: Some("percent".to_string()),
1441+
unit: Some("percent".into()),
14431442
description: None,
1444-
href: "/properties/humidity".to_string(),
1443+
href: "/properties/humidity".into(),
14451444
},
14461445
led: Property {
14471446
ty: Type::Boolean,
14481447
unit: None,
1449-
description: Some("A red LED".to_string()),
1450-
href: "/properties/led".to_string(),
1448+
description: Some("A red LED".into()),
1449+
href: "/properties/led".into(),
14511450
},
14521451
},
14531452
ty: Type::Thing,

0 commit comments

Comments
 (0)