mirror of
https://github.com/serde-rs/json.git
synced 2025-11-17 13:56:10 +00:00
Reorganize std::io facade
This commit is contained in:
parent
9ecf91504a
commit
15bfaf6a07
60
src/io.rs
60
src/io.rs
@ -1,15 +1,33 @@
|
|||||||
|
//! A tiny, `no_std`-friendly facade around `std::io`.
|
||||||
|
//! Reexports types from `std` when available; otherwise reimplements and
|
||||||
|
//! provides some of the core logic.
|
||||||
|
//!
|
||||||
|
//! The main reason that `std::io` hasn't found itself reexported as part of
|
||||||
|
//! the `core` crate is the `std::io::{Read, Write}` traits' reliance on
|
||||||
|
//! `std::io::Error`, which may contain internally a heap-allocated `Box<Error>`
|
||||||
|
//! and/or now relying on OS-specific `std::backtrace::Backtrace`.
|
||||||
|
//!
|
||||||
|
//! Because of this, we simply redefine those traits as if the error type is
|
||||||
|
//! simply a `&'static str` and reimplement those traits for `core` primitives
|
||||||
|
//! or `alloc` types, e.g. `Vec<T>`.
|
||||||
#[cfg(not(feature = "std"))]
|
#[cfg(not(feature = "std"))]
|
||||||
use core::slice;
|
use lib::*;
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
pub use std::io::{Result, Write, Read, Error, Bytes, ErrorKind};
|
pub use std::io::ErrorKind;
|
||||||
|
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
pub use std::io::Error;
|
||||||
#[cfg(not(feature = "std"))]
|
#[cfg(not(feature = "std"))]
|
||||||
pub type Error = &'static str;
|
pub type Error = &'static str;
|
||||||
|
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
pub use std::io::Result;
|
||||||
#[cfg(not(feature = "std"))]
|
#[cfg(not(feature = "std"))]
|
||||||
pub type Result<T> = core::result::Result<T, Error>;
|
pub type Result<T> = core::result::Result<T, Error>;
|
||||||
|
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
pub use std::io::Write;
|
||||||
#[cfg(not(feature = "std"))]
|
#[cfg(not(feature = "std"))]
|
||||||
pub trait Write {
|
pub trait Write {
|
||||||
fn write(&mut self, buf: &[u8]) -> Result<usize>;
|
fn write(&mut self, buf: &[u8]) -> Result<usize>;
|
||||||
@ -30,35 +48,57 @@ pub trait Write {
|
|||||||
|
|
||||||
#[cfg(not(feature = "std"))]
|
#[cfg(not(feature = "std"))]
|
||||||
impl<W: Write> Write for &mut W {
|
impl<W: Write> Write for &mut W {
|
||||||
|
#[inline]
|
||||||
fn write(&mut self, buf: &[u8]) -> Result<usize> {
|
fn write(&mut self, buf: &[u8]) -> Result<usize> {
|
||||||
(*self).write(buf)
|
(*self).write(buf)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn write_all(&mut self, buf: &[u8]) -> Result<()> {
|
||||||
|
(*self).write_all(buf)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
fn flush(&mut self) -> Result<()> {
|
fn flush(&mut self) -> Result<()> {
|
||||||
(*self).flush()
|
(*self).flush()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(all(not(feature = "std"), feature = "alloc"))]
|
#[cfg(not(feature = "std"))]
|
||||||
impl Write for &mut serde::export::Vec<u8> {
|
impl Write for Vec<u8> {
|
||||||
|
#[inline]
|
||||||
fn write(&mut self, buf: &[u8]) -> Result<usize> {
|
fn write(&mut self, buf: &[u8]) -> Result<usize> {
|
||||||
self.extend(buf);
|
self.extend_from_slice(buf);
|
||||||
Ok(buf.len())
|
Ok(buf.len())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn flush(&mut self) -> Result<()> {Ok(())}
|
#[inline]
|
||||||
|
fn write_all(&mut self, buf: &[u8]) -> Result<()> {
|
||||||
|
self.extend_from_slice(buf);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn flush(&mut self) -> Result<()> {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
pub use std::io::Read;
|
||||||
#[cfg(not(feature = "std"))]
|
#[cfg(not(feature = "std"))]
|
||||||
pub trait Read {
|
pub trait Read {
|
||||||
fn read(&mut self, buf: &mut [u8]) -> Result<usize>;
|
fn read(&mut self, buf: &mut [u8]) -> Result<usize>;
|
||||||
fn bytes(self) -> Bytes<Self> where Self: Sized {
|
fn bytes(self) -> Bytes<Self>
|
||||||
Bytes {
|
where
|
||||||
inner: self,
|
Self: Sized,
|
||||||
}
|
{
|
||||||
|
Bytes { inner: self }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
pub use std::io::Bytes;
|
||||||
#[cfg(not(feature = "std"))]
|
#[cfg(not(feature = "std"))]
|
||||||
pub struct Bytes<R> {
|
pub struct Bytes<R> {
|
||||||
inner: R,
|
inner: R,
|
||||||
|
|||||||
@ -199,10 +199,12 @@ impl<'a, 'b> io::Write for WriterFormatter<'a, 'b> {
|
|||||||
// maps it to fmt::Error
|
// maps it to fmt::Error
|
||||||
io::Error::new(io::ErrorKind::Other, "fmt error")
|
io::Error::new(io::ErrorKind::Other, "fmt error")
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(feature = "std"))]
|
#[cfg(not(feature = "std"))]
|
||||||
fn io_error<E>(_: E) -> &'static str {
|
fn io_error<E>(_: E) -> io::Error {
|
||||||
"fmt error"
|
"fmt error"
|
||||||
}
|
}
|
||||||
|
|
||||||
let s = try!(str::from_utf8(buf).map_err(io_error));
|
let s = try!(str::from_utf8(buf).map_err(io_error));
|
||||||
try!(self.inner.write_str(s).map_err(io_error));
|
try!(self.inner.write_str(s).map_err(io_error));
|
||||||
Ok(buf.len())
|
Ok(buf.len())
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
use lib::String;
|
use lib::*;
|
||||||
|
|
||||||
use super::Value;
|
use super::Value;
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user