Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #606 from Xanewok/no-std-patch
Add a no_std/alloc feature
- Loading branch information
Showing
21 changed files
with
340 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"serde_json doesn't work without `alloc` or default `std` feature yet" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
//! Shows a user-friendly compiler error on incompatible selected features. | ||
|
||
#[allow(unused_macros)] | ||
macro_rules! hide_from_rustfmt { | ||
($mod:item) => { | ||
$mod | ||
}; | ||
} | ||
|
||
#[cfg(all(not(feature = "std"), not(feature = "alloc")))] | ||
hide_from_rustfmt! { | ||
mod error; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
//! Reimplements core logic and types from `std::io` in an `alloc`-friendly | ||
//! fashion. | ||
#![cfg(not(feature = "std"))] | ||
|
||
use lib::*; | ||
|
||
pub enum ErrorKind { | ||
InvalidData, | ||
WriteZero, | ||
Other, | ||
UnexpectedEof, | ||
} | ||
|
||
impl ErrorKind { | ||
#[inline] | ||
fn as_str(&self) -> &'static str { | ||
match self { | ||
ErrorKind::InvalidData => "invalid data", | ||
ErrorKind::WriteZero => "write zero", | ||
ErrorKind::Other => "other os error", | ||
ErrorKind::UnexpectedEof => "unexpected end of file", | ||
} | ||
} | ||
} | ||
|
||
pub struct Error { | ||
repr: Repr, | ||
} | ||
|
||
enum Repr { | ||
Simple(ErrorKind), | ||
Custom(ErrorKind, Box<dyn serde::de::StdError + Send + Sync>), | ||
} | ||
|
||
impl Display for Error { | ||
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
match &self.repr { | ||
Repr::Custom(_, msg) => write!(fmt, "{}", msg), | ||
Repr::Simple(kind) => write!(fmt, "{}", kind.as_str()), | ||
} | ||
} | ||
} | ||
|
||
impl Debug for Error { | ||
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
Display::fmt(self, fmt) | ||
} | ||
} | ||
|
||
impl serde::de::StdError for Error {} | ||
|
||
impl From<ErrorKind> for Error { | ||
#[inline] | ||
fn from(kind: ErrorKind) -> Error { | ||
Error { | ||
repr: Repr::Simple(kind), | ||
} | ||
} | ||
} | ||
|
||
impl Error { | ||
#[inline] | ||
pub fn new<E>(kind: ErrorKind, error: E) -> Error | ||
where | ||
E: Into<Box<dyn serde::de::StdError + Send + Sync>>, | ||
{ | ||
Error { | ||
repr: Repr::Custom(kind, error.into()), | ||
} | ||
} | ||
} | ||
|
||
pub type Result<T> = result::Result<T, Error>; | ||
|
||
pub trait Write { | ||
fn write(&mut self, buf: &[u8]) -> Result<usize>; | ||
|
||
fn write_all(&mut self, mut buf: &[u8]) -> Result<()> { | ||
while !buf.is_empty() { | ||
match self.write(buf) { | ||
Ok(0) => { | ||
return Err(Error::new( | ||
ErrorKind::WriteZero, | ||
"failed to write whole buffer", | ||
)) | ||
} | ||
Ok(n) => buf = &buf[n..], | ||
Err(e) => return Err(e), | ||
} | ||
} | ||
Ok(()) | ||
} | ||
|
||
fn flush(&mut self) -> Result<()>; | ||
} | ||
|
||
impl<W: Write> Write for &mut W { | ||
#[inline] | ||
fn write(&mut self, buf: &[u8]) -> Result<usize> { | ||
(*self).write(buf) | ||
} | ||
|
||
#[inline] | ||
fn write_all(&mut self, buf: &[u8]) -> Result<()> { | ||
(*self).write_all(buf) | ||
} | ||
|
||
#[inline] | ||
fn flush(&mut self) -> Result<()> { | ||
(*self).flush() | ||
} | ||
} | ||
|
||
impl Write for Vec<u8> { | ||
#[inline] | ||
fn write(&mut self, buf: &[u8]) -> Result<usize> { | ||
self.extend_from_slice(buf); | ||
Ok(buf.len()) | ||
} | ||
|
||
#[inline] | ||
fn write_all(&mut self, buf: &[u8]) -> Result<()> { | ||
self.extend_from_slice(buf); | ||
Ok(()) | ||
} | ||
|
||
#[inline] | ||
fn flush(&mut self) -> Result<()> { | ||
Ok(()) | ||
} | ||
} | ||
|
||
pub trait Read { | ||
fn read(&mut self, buf: &mut [u8]) -> Result<usize>; | ||
fn bytes(self) -> Bytes<Self> | ||
where | ||
Self: Sized, | ||
{ | ||
Bytes { inner: self } | ||
} | ||
} | ||
|
||
pub struct Bytes<R> { | ||
inner: R, | ||
} | ||
|
||
impl<R: Read> Iterator for Bytes<R> { | ||
type Item = Result<u8>; | ||
|
||
fn next(&mut self) -> Option<Result<u8>> { | ||
let mut byte = 0; | ||
match self.inner.read(slice::from_mut(&mut byte)) { | ||
Ok(0) => None, | ||
Ok(..) => Some(Ok(byte)), | ||
Err(e) => Some(Err(e)), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
//! 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`. | ||
|
||
pub use self::imp::{Bytes, Error, ErrorKind, Read, Result, Write}; | ||
|
||
mod core; | ||
|
||
mod imp { | ||
#[cfg(not(feature = "std"))] | ||
pub use super::core::*; | ||
#[cfg(feature = "std")] | ||
pub use std::io::*; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
use std::io; | ||
use io; | ||
|
||
pub struct LineColIterator<I> { | ||
iter: I, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.