Skip to content

Commit a2c4cf0

Browse files
bors[bot]japaric
andcommitted
3: add a std feature r=japaric a=japaric Co-authored-by: Jorge Aparicio <jorge@japaric.io>
2 parents 3f8f4b6 + 11e7dca commit a2c4cf0

File tree

5 files changed

+33
-13
lines changed

5 files changed

+33
-13
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@ version = "1.0.33"
1212

1313
[dev-dependencies]
1414
serde_derive = "1.0.33"
15+
16+
[features]
17+
std = ["serde/std"]

ci/script.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ main() {
55

66
if [ $TARGET = x86_64-unknown-linux-gnu ]; then
77
cargo test --target $TARGET
8+
cargo check --target $TARGET --features std
89
return
910
fi
1011
}

src/de/mod.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ pub enum Error {
6767
__Extensible,
6868
}
6969

70+
#[cfg(feature = "std")]
71+
impl ::std::error::Error for Error {
72+
fn description(&self) -> &str {
73+
""
74+
}
75+
}
76+
7077
pub(crate) struct Deserializer<'b> {
7178
slice: &'b [u8],
7279
index: usize,
@@ -185,7 +192,7 @@ impl<'a> Deserializer<'a> {
185192
// is what upstream does, to avoid pulling in 64-bit compiler intrinsics, which waste a few KBs of
186193
// Flash, when targeting non 64-bit architectures
187194
macro_rules! deserialize_unsigned {
188-
($self:ident, $visitor:ident, $uxx:ident, $visit_uxx:ident) => ({
195+
($self:ident, $visitor:ident, $uxx:ident, $visit_uxx:ident) => {{
189196
let peek = $self.parse_whitespace().ok_or(Error::EofWhileParsingValue)?;
190197

191198
match peek {
@@ -205,7 +212,7 @@ macro_rules! deserialize_unsigned {
205212
number = number
206213
.checked_mul(10)
207214
.ok_or(Error::InvalidNumber)?
208-
.checked_add((c - b'0') as $uxx)
215+
.checked_add((c - b'0') as $uxx)
209216
.ok_or(Error::InvalidNumber)?;
210217
}
211218
_ => return $visitor.$visit_uxx(number),
@@ -214,11 +221,11 @@ macro_rules! deserialize_unsigned {
214221
}
215222
_ => Err(Error::InvalidType),
216223
}
217-
})
224+
}};
218225
}
219226

220227
macro_rules! deserialize_signed {
221-
($self:ident, $visitor:ident, $ixx:ident, $visit_ixx:ident) => ({
228+
($self:ident, $visitor:ident, $ixx:ident, $visit_ixx:ident) => {{
222229
let signed = match $self.parse_whitespace().ok_or(Error::EofWhileParsingValue)? {
223230
b'-' => {
224231
$self.eat_char();
@@ -252,7 +259,7 @@ macro_rules! deserialize_signed {
252259
}
253260
_ => return Err(Error::InvalidType),
254261
}
255-
})
262+
}};
256263
}
257264

258265
impl<'a, 'de> de::Deserializer<'de> for &'a mut Deserializer<'de> {

src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@
5050
#![deny(missing_docs)]
5151
#![deny(warnings)]
5252
#![feature(unsize)]
53-
#![no_std]
53+
#![cfg_attr(not(feature = "std"), no_std)]
5454

55+
#[cfg(feature = "std")]
56+
extern crate core;
5557
extern crate heapless;
5658
extern crate serde;
5759
#[cfg(test)]

src/ser/mod.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use heapless::{BufferFullError, String, Vec};
1010
use self::seq::SerializeSeq;
1111
use self::struct_::SerializeStruct;
1212

13-
mod struct_;
1413
mod seq;
14+
mod struct_;
1515

1616
/// Serialization result
1717
pub type Result<T> = ::core::result::Result<T, Error>;
@@ -25,6 +25,13 @@ pub enum Error {
2525
__Extensible,
2626
}
2727

28+
#[cfg(feature = "std")]
29+
impl ::std::error::Error for Error {
30+
fn description(&self) -> &str {
31+
""
32+
}
33+
}
34+
2835
impl From<BufferFullError> for Error {
2936
fn from(_: BufferFullError) -> Self {
3037
Error::BufferFull
@@ -56,29 +63,29 @@ where
5663
// NOTE(serialize_*signed) This is basically the numtoa implementation minus the lookup tables,
5764
// which take 200+ bytes of ROM / Flash
5865
macro_rules! serialize_unsigned {
59-
($self:ident, $N:expr, $v:expr) => ({
66+
($self:ident, $N:expr, $v:expr) => {{
6067
let mut buf: [u8; $N] = unsafe { mem::uninitialized() };
6168

6269
let mut v = $v;
6370
let mut i = $N - 1;
64-
loop {
71+
loop {
6572
buf[i] = (v % 10) as u8 + b'0';
6673
v /= 10;
6774

6875
if v == 0 {
69-
break
76+
break;
7077
} else {
7178
i -= 1;
7279
}
7380
}
7481

7582
$self.buf.extend_from_slice(&buf[i..])?;
7683
Ok(())
77-
})
84+
}};
7885
}
7986

8087
macro_rules! serialize_signed {
81-
($self:ident, $N:expr, $v:expr, $ixx:ident, $uxx:ident) => ({
88+
($self:ident, $N:expr, $v:expr, $ixx:ident, $uxx:ident) => {{
8289
let v = $v;
8390
let (signed, mut v) = if v == $ixx::min_value() {
8491
(true, $ixx::max_value() as $uxx + 1)
@@ -108,7 +115,7 @@ macro_rules! serialize_signed {
108115
}
109116
$self.buf.extend_from_slice(&buf[i..])?;
110117
Ok(())
111-
})
118+
}};
112119
}
113120

114121
impl<'a, B> ser::Serializer for &'a mut Serializer<B>

0 commit comments

Comments
 (0)