Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make all serialization functions available for no-std #1122

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions Cargo.toml
Expand Up @@ -16,6 +16,7 @@ indexmap = { version = "2.2.1", optional = true }
itoa = "1.0"
ryu = "1.0"
serde = { version = "1.0.194", default-features = false }
core2 = { version = "0.4", optional = true }

[dev-dependencies]
automod = "1.0.11"
Expand All @@ -39,6 +40,9 @@ rustdoc-args = ["--cfg", "docsrs", "--generate-link-to-definition"]
[package.metadata.playground]
features = ["raw_value"]

[[example]]
name = "test"
required-features = ["use-core2"]

### FEATURES #################################################################

Expand Down Expand Up @@ -86,3 +90,5 @@ raw_value = []
# overflow the stack after deserialization has completed, including, but not
# limited to, Display and Debug and Drop impls.
unbounded_depth = []

use-core2 = ["core2"]
30 changes: 30 additions & 0 deletions examples/test.rs
@@ -0,0 +1,30 @@
//! Tests feature unification
//! Run via:
//!
//! ```sh
//! cargo run --example test --features use-core2
//! cargo run --example test --features alloc,use-core2 --no-default-features
//! ```


use serde_json::alloc_io::{Write, Result, Error, ErrorKind};


struct Buffer {}

impl Write for Buffer {
fn write(&mut self, _bytes: &[u8]) -> Result<usize> { panic!() }
fn flush(&mut self) -> Result<()> {
Err(Error::new(ErrorKind::Other, "flush not implemented"))
}
}

impl core2::io::Write for Buffer {
fn write(&mut self, _bytes: &[u8]) -> core2::io::Result<usize> { panic!() }
fn flush(&mut self) -> core2::io::Result<()> { panic!() }
}

fn main() {
println!("Hello, world!");
let _x = &mut Buffer {} as &mut dyn Write;
}
17 changes: 14 additions & 3 deletions src/io/core.rs
Expand Up @@ -5,13 +5,18 @@ use alloc::vec::Vec;
use core::fmt::{self, Display};
use core::result;

/// see [`std::io::ErrorKind`]
pub enum ErrorKind {
/// see [`std::io::ErrorKind::Other`]
Other,
}

/// see [`std::io::Error`]
// I/O errors can never occur in no-std mode. All our no-std I/O implementations
// are infallible.
pub struct Error;
pub struct Error {
_priv: (),
}

impl Display for Error {
fn fmt(&self, _formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand All @@ -20,16 +25,21 @@ impl Display for Error {
}

impl Error {
pub(crate) fn new(_kind: ErrorKind, _error: &'static str) -> Error {
Error
/// see [`std::io::Error::new`]
pub fn new(_kind: ErrorKind, _error: &'static str) -> Error {
Error { _priv: () }
}
}

/// see [`std::io::Result`]
pub type Result<T> = result::Result<T, Error>;

/// see [`std::io::Write`]
pub trait Write {
/// see [`std::io::Write::write`]
fn write(&mut self, buf: &[u8]) -> Result<usize>;

/// see [`std::io::Write::write_all`]
fn write_all(&mut self, buf: &[u8]) -> Result<()> {
// All our Write impls in no_std mode always write the whole buffer in
// one call infallibly.
Expand All @@ -39,6 +49,7 @@ pub trait Write {
Ok(())
}

/// see [`std::io::Write::flush`]
fn flush(&mut self) -> Result<()>;
}

Expand Down
6 changes: 4 additions & 2 deletions src/io/mod.rs
Expand Up @@ -9,9 +9,11 @@

pub use self::imp::{Error, ErrorKind, Result, Write};

// no-std implementation
pub mod core;

#[cfg(not(feature = "std"))]
#[path = "core.rs"]
mod imp;
use core as imp;

#[cfg(feature = "std")]
use std::io as imp;
Expand Down
11 changes: 2 additions & 9 deletions src/lib.rs
Expand Up @@ -377,11 +377,7 @@ pub use crate::de::{from_slice, from_str, Deserializer, StreamDeserializer};
#[doc(inline)]
pub use crate::error::{Error, Result};
#[doc(inline)]
pub use crate::ser::{to_string, to_string_pretty, to_vec, to_vec_pretty};
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
#[doc(inline)]
pub use crate::ser::{to_writer, to_writer_pretty, Serializer};
pub use crate::ser::{to_string, to_string_pretty, to_vec, to_vec_pretty, to_writer, to_writer_pretty, Serializer};
#[doc(inline)]
pub use crate::value::{from_value, to_value, Map, Number, Value};

Expand All @@ -402,15 +398,12 @@ mod macros;
pub mod de;
pub mod error;
pub mod map;
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
pub mod ser;
#[cfg(not(feature = "std"))]
mod ser;
pub mod value;

mod features_check;

pub use io::core as alloc_io;
mod io;
#[cfg(feature = "std")]
mod iter;
Expand Down