Skip to content

Commit 05649f4

Browse files
committed
no_std
1 parent 6b80e7c commit 05649f4

22 files changed

+2393
-37
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
[workspace]
22
members = [
3+
"nostd",
4+
"num-traits",
35
"rmp",
46
"rmp-serde",
57
"rmp-serialize",

nostd/Cargo.toml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[package]
2+
name = "nostd"
3+
version = "0.1.0"
4+
authors = ["Vadzim Dambrouski <pftbest@gmail.com>"]
5+
6+
[dependencies.byteorder]
7+
version = "1"
8+
default-features = false
9+
10+
[dependencies.arrayvec]
11+
version = "0.4"
12+
default-features = false
13+
14+
[features]
15+
default = []
16+
# this feature is used for tests `cargo test --features=std`
17+
std = []

nostd/src/error.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use core::fmt::{Debug, Display};
2+
use core::str::Utf8Error;
3+
4+
pub trait Error: Debug + Display {
5+
fn description(&self) -> &str;
6+
fn cause(&self) -> Option<&Error> { None }
7+
}
8+
9+
impl Error for Utf8Error {
10+
fn description(&self) -> &str {
11+
"Utf8Error"
12+
}
13+
14+
fn cause(&self) -> Option<&Error> {
15+
None
16+
}
17+
}

nostd/src/io/cursor.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use core::cmp;
2+
use io::{Read, Result};
3+
4+
#[derive(Clone, Debug)]
5+
pub struct Cursor<T> {
6+
inner: T,
7+
pos: u64,
8+
}
9+
10+
impl<T> Cursor<T> {
11+
pub fn new(inner: T) -> Cursor<T> {
12+
Cursor { pos: 0, inner: inner }
13+
}
14+
15+
pub fn position(&self) -> u64 { self.pos }
16+
}
17+
18+
impl<T> Cursor<T> where T: AsRef<[u8]> {
19+
fn fill_buf(&mut self) -> Result<&[u8]> {
20+
let amt = cmp::min(self.pos, self.inner.as_ref().len() as u64);
21+
Ok(&self.inner.as_ref()[(amt as usize)..])
22+
}
23+
}
24+
25+
impl<T> Read for Cursor<T> where T: AsRef<[u8]> {
26+
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
27+
let n = Read::read(&mut self.fill_buf()?, buf)?;
28+
self.pos += n as u64;
29+
Ok(n)
30+
}
31+
}

nostd/src/io/error.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
use core::{result, fmt};
2+
use core::fmt::{Display, Formatter};
3+
use error;
4+
5+
pub type Result<T> = result::Result<T, Error>;
6+
7+
#[derive(Debug)]
8+
pub struct Error {
9+
reason: &'static str,
10+
}
11+
12+
impl Error {
13+
pub fn new(reason: &'static str) -> Self {
14+
Error {
15+
reason: reason,
16+
}
17+
}
18+
}
19+
20+
impl error::Error for Error {
21+
fn description(&self) -> &str {
22+
self.reason
23+
}
24+
25+
fn cause(&self) -> Option<&error::Error> {
26+
None
27+
}
28+
}
29+
30+
impl Display for Error {
31+
fn fmt(&self, f: &mut Formatter) -> result::Result<(), fmt::Error> {
32+
error::Error::description(self).fmt(f)
33+
}
34+
}
35+
36+
#[cfg(feature = "std")]
37+
impl From<::std::io::Error> for Error {
38+
fn from(_err: ::std::io::Error) -> Self {
39+
return ::io::Error { reason: "IO Error" };
40+
}
41+
}

nostd/src/io/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
mod read;
2+
mod write;
3+
mod error;
4+
mod cursor;
5+
6+
pub use self::read::Read;
7+
pub use self::write::Write;
8+
pub use self::error::{Error, Result};
9+
pub use self::cursor::Cursor;

nostd/src/io/read.rs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
use core::cmp;
2+
use io::{Error, Result};
3+
4+
pub trait Read {
5+
fn read(&mut self, buf: &mut [u8]) -> Result<usize>;
6+
fn read_exact(&mut self, mut buf: &mut [u8]) -> Result<()> {
7+
while !buf.is_empty() {
8+
match self.read(buf) {
9+
Ok(0) => break,
10+
Ok(n) => { let tmp = buf; buf = &mut tmp[n..]; }
11+
Err(e) => return Err(e),
12+
}
13+
}
14+
if !buf.is_empty() {
15+
Err(Error::new("failed to fill whole buffer"))
16+
} else {
17+
Ok(())
18+
}
19+
}
20+
}
21+
22+
impl<'a, R: Read + ?Sized> Read for &'a mut R {
23+
#[inline]
24+
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
25+
(**self).read(buf)
26+
}
27+
#[inline]
28+
fn read_exact(&mut self, buf: &mut [u8]) -> Result<()> {
29+
(**self).read_exact(buf)
30+
}
31+
}
32+
33+
impl<'a> Read for &'a [u8] {
34+
#[inline]
35+
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
36+
let amt = cmp::min(buf.len(), self.len());
37+
let (a, b) = self.split_at(amt);
38+
39+
// First check if the amount of bytes we want to read is small:
40+
// `copy_from_slice` will generally expand to a call to `memcpy`, and
41+
// for a single byte the overhead is significant.
42+
if amt == 1 {
43+
buf[0] = a[0];
44+
} else {
45+
buf[..amt].copy_from_slice(a);
46+
}
47+
48+
*self = b;
49+
Ok(amt)
50+
}
51+
#[inline]
52+
fn read_exact(&mut self, buf: &mut [u8]) -> Result<()> {
53+
if buf.len() > self.len() {
54+
return Err(Error::new("failed to fill whole buffer"));
55+
}
56+
let (a, b) = self.split_at(buf.len());
57+
58+
// First check if the amount of bytes we want to read is small:
59+
// `copy_from_slice` will generally expand to a call to `memcpy`, and
60+
// for a single byte the overhead is significant.
61+
if buf.len() == 1 {
62+
buf[0] = a[0];
63+
} else {
64+
buf.copy_from_slice(a);
65+
}
66+
67+
*self = b;
68+
Ok(())
69+
}
70+
}
71+
72+
#[cfg(feature = "std")]
73+
impl<T> Read for ::std::io::Cursor<T> where T: AsRef<[u8]> {
74+
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
75+
::std::io::Read::read(self, buf).map_err(Into::into)
76+
}
77+
}

nostd/src/io/write.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
use core::{mem, cmp};
2+
use io::{Result, Error};
3+
4+
pub trait Write {
5+
fn write(&mut self, buf: &[u8]) -> Result<usize>;
6+
fn write_all(&mut self, buf: &[u8]) -> Result<()>;
7+
}
8+
9+
impl<'a, W: Write + ?Sized> Write for &'a mut W {
10+
#[inline]
11+
fn write(&mut self, buf: &[u8]) -> Result<usize> {
12+
(**self).write(buf)
13+
}
14+
#[inline]
15+
fn write_all(&mut self, buf: &[u8]) -> Result<()> {
16+
(**self).write_all(buf)
17+
}
18+
}
19+
20+
impl<'a> Write for &'a mut [u8] {
21+
#[inline]
22+
fn write(&mut self, data: &[u8]) -> Result<usize> {
23+
let amt = cmp::min(data.len(), self.len());
24+
let (a, b) = mem::replace(self, &mut []).split_at_mut(amt);
25+
a.copy_from_slice(&data[..amt]);
26+
*self = b;
27+
Ok(amt)
28+
}
29+
30+
#[inline]
31+
fn write_all(&mut self, data: &[u8]) -> Result<()> {
32+
if self.write(data)? == data.len() {
33+
Ok(())
34+
} else {
35+
Err(Error::new("failed to write whole buffer"))
36+
}
37+
}
38+
}
39+
40+
#[cfg(feature = "std")]
41+
impl Write for ::std::vec::Vec<u8> {
42+
fn write(&mut self, data: &[u8]) -> Result<usize> {
43+
self.extend_from_slice(data);
44+
Ok(data.len())
45+
}
46+
fn write_all(&mut self, data: &[u8]) -> Result<()> {
47+
self.extend_from_slice(data);
48+
Ok(())
49+
}
50+
}

0 commit comments

Comments
 (0)