Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ exclude = [
".gitignore",
]

[features]
default = ["std"]
std = []

[dependencies]
serde = { version = "^1.0.80", default-features = false, features = ["alloc"] }

Expand Down
11 changes: 11 additions & 0 deletions rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[toolchain]
channel = "nightly-2022-11-17"
components = [
"rust-src",
"cargo",
]
targets = [
"wasm32-unknown-unknown",
# that is the way to test that it checks and builds againt no_std target
"thumbv7em-none-eabi"
]
23 changes: 23 additions & 0 deletions shell.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{ pkgs ? import <nixpkgs> { overlays = [ (import (builtins.fetchTarball "https://github.com/oxalica/rust-overlay/archive/refs/heads/stable.zip")) ]; } }:
let
rust-as-on-ci = pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml;
package = "serde-json-wasm";
check-no-std = pkgs.writeShellApplication rec {
name = "check-no-std";
runtimeInputs = [ rust-as-on-ci ];
text = ''
cargo build --no-default-features --target thumbv7em-none-eabi --package ${package}
cargo build --no-default-features --target wasm32-unknown-unknown --package ${package}
'';
};
check-wasm-std = pkgs.writeShellApplication rec {
name = "check-wasm-std";
runtimeInputs = [ rust-as-on-ci ];
text = ''
cargo build --target wasm32-unknown-unknown --locked --package ${package} --features std
'';
};
in
pkgs.mkShell {
nativeBuildInputs = [ rust-as-on-ci check-no-std check-wasm-std];
}
3 changes: 2 additions & 1 deletion src/de/errors.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::no_std::prelude::*;
use crate::no_std::{error, fmt};
use serde::de;
use std::{error, fmt};

/// Deserialization result
pub type Result<T> = core::result::Result<T, Error>;
Expand Down
3 changes: 2 additions & 1 deletion src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ use serde::de::{self, Visitor};
use self::enum_::{StructVariantAccess, UnitVariantAccess};
use self::map::MapAccess;
use self::seq::SeqAccess;
use std::str::from_utf8;
use crate::no_std::prelude::*;
use crate::no_std::str::from_utf8;

/// Deserializer will parse serde-json-wasm flavored JSON into a
/// serde-annotated struct
Expand Down
3 changes: 2 additions & 1 deletion src/de/unescape.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::convert::TryFrom;
use crate::no_std::convert::TryFrom;
use crate::no_std::prelude::*;

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

Expand Down
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,14 @@
#![deny(missing_docs)]
#![deny(rust_2018_compatibility)]
#![deny(rust_2018_idioms)]
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(not(feature = "std"), feature(error_in_core))]

#[cfg(not(feature = "std"))]
extern crate alloc;

pub mod de;
mod no_std;
pub mod ser;

#[doc(inline)]
Expand Down
109 changes: 109 additions & 0 deletions src/no_std.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
//! if somebody will need alternative implementation, for example for collection or fmt, one may patch for himself this one

#[cfg(feature = "std")]
pub use std::vec;

#[cfg(not(feature = "std"))]
pub use alloc::vec;

#[cfg(not(feature = "std"))]
pub use core::mem;
#[cfg(feature = "std")]
pub use std::mem;

#[cfg(not(feature = "std"))]
pub use core::result;
#[cfg(feature = "std")]
pub use std::result;

#[cfg(not(feature = "std"))]
pub use core::cmp;
#[cfg(feature = "std")]
pub use std::cmp;

#[cfg(not(feature = "std"))]
pub use core::error;
#[cfg(feature = "std")]
pub use std::error;

#[cfg(feature = "std")]
pub use std::iter;

#[cfg(not(feature = "std"))]
pub use core::iter;

#[cfg(feature = "std")]
pub use std::ops;

#[cfg(not(feature = "std"))]
pub use core::ops;

#[cfg(feature = "std")]
pub use std::string;

#[cfg(not(feature = "std"))]
pub use alloc::string;

pub mod array {
#[cfg(feature = "std")]
pub use std::array::TryFromSliceError;

#[cfg(not(feature = "std"))]
pub use core::array::TryFromSliceError;
}

#[cfg(feature = "std")]
pub use std::convert;

#[cfg(not(feature = "std"))]
pub use core::convert;

#[cfg(feature = "std")]
pub use std::collections;

#[cfg(not(feature = "std"))]
pub use alloc::collections;

#[cfg(feature = "std")]
pub use std::fmt;

#[cfg(not(feature = "std"))]
pub use alloc::fmt;

pub mod borrow {
#[cfg(feature = "std")]
pub use std::borrow::Cow;

#[cfg(not(feature = "std"))]
pub use alloc::borrow::Cow;
}

#[cfg(feature = "std")]
pub use std::str;

#[cfg(not(feature = "std"))]
pub use alloc::str;

#[cfg(feature = "std")]
pub use std::marker;

#[cfg(not(feature = "std"))]
pub use core::marker;

pub mod any {
#[cfg(feature = "std")]
pub use std::any::type_name;

#[cfg(not(feature = "std"))]
pub use core::any::type_name;
}

// just because it is default prelude
pub mod prelude {
pub use super::{
marker::{Send, Sync},
result::Result::{Err, Ok},
string::{String, ToString},
vec::Vec,
};
}
3 changes: 2 additions & 1 deletion src/ser/map.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::fmt;
use crate::no_std::fmt;
use crate::no_std::prelude::*;

use serde::{ser, Serialize};

Expand Down
5 changes: 3 additions & 2 deletions src/ser/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
//! Serialize a Rust data structure into JSON data

use std::{error, fmt};
use crate::no_std::prelude::*;
use crate::no_std::{error, fmt};

use serde::ser;

use std::vec::Vec;
use crate::no_std::vec::Vec;

use self::map::SerializeMap;
use self::seq::SerializeSeq;
Expand Down