Skip to content

Commit 2f047fc

Browse files
committed
Add core IO using core2 crate
1 parent 2ec58b6 commit 2f047fc

File tree

6 files changed

+92
-22
lines changed

6 files changed

+92
-22
lines changed

Cargo.lock

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ sha-2 = { version = "0.9.0", default-features = false, optional = true, package
5050
sha-3 = { version = "0.9.0", default-features = false, optional = true, package = "sha3" }
5151
strobe-rs = { version = "0.6.2", default-features = false, optional = true }
5252

53+
core2 = { version = "0.3", default-features = false, features = ["alloc"] }
54+
5355
[dev-dependencies]
5456
criterion = "0.3.3"
5557
hex = "0.4.2"

src/error.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#[cfg(feature = "std")]
22
use std::io::Error as IoError;
3+
#[cfg(not(feature = "std"))]
4+
use core2::io::Error as IoError;
5+
36
use unsigned_varint::decode::Error as DecodeError;
47
#[cfg(feature = "std")]
58
use unsigned_varint::io::ReadError;
@@ -8,7 +11,6 @@ use unsigned_varint::io::ReadError;
811
#[derive(Debug)]
912
pub enum Error {
1013
/// Io error.
11-
#[cfg(feature = "std")]
1214
Io(IoError),
1315
/// Unsupported multihash code.
1416
UnsupportedCode(u64),
@@ -21,7 +23,7 @@ pub enum Error {
2123
impl core::fmt::Display for Error {
2224
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
2325
match self {
24-
#[cfg(feature = "std")]
26+
//#[cfg(feature = "std")]
2527
Self::Io(err) => write!(f, "{}", err),
2628
Self::UnsupportedCode(code) => write!(f, "Unsupported multihash code {}.", code),
2729
Self::InvalidSize(size) => write!(f, "Invalid multihash size {}.", size),
@@ -33,6 +35,16 @@ impl core::fmt::Display for Error {
3335
#[cfg(feature = "std")]
3436
impl std::error::Error for Error {}
3537

38+
#[cfg(not(feature = "std"))]
39+
impl core2::error::Error for Error {}
40+
41+
#[cfg(not(feature = "std"))]
42+
impl From<core2::io::Error> for Error {
43+
fn from(err: core2::io::Error) -> Self {
44+
Self::Io(err)
45+
}
46+
}
47+
3648
#[cfg(feature = "std")]
3749
impl From<IoError> for Error {
3850
fn from(err: IoError) -> Self {

src/hasher.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ use core::fmt::Debug;
33
use generic_array::typenum::marker_traits::Unsigned;
44
use generic_array::{ArrayLength, GenericArray};
55

6+
#[cfg(feature = "std")]
7+
use std::io;
8+
9+
#[cfg(not(feature = "std"))]
10+
use core2::io;
11+
612
/// Size marker trait.
713
pub trait Size:
814
ArrayLength<u8> + Debug + Default + Eq + core::hash::Hash + Send + Sync + 'static
@@ -48,12 +54,15 @@ pub trait Digest<S: Size>:
4854
/// Reads a multihash digest from a byte stream that contains the digest prefixed with the size.
4955
///
5056
/// The byte stream must not contain the code as prefix.
51-
#[cfg(feature = "std")]
5257
fn from_reader<R>(mut r: R) -> Result<Self, Error>
5358
where
54-
R: std::io::Read,
59+
R: io::Read,
5560
{
56-
use unsigned_varint::io::read_u64;
61+
#[cfg(not(feature = "std"))]
62+
use crate::varint_read_u64 as read_u64;
63+
64+
#[cfg(feature = "std")]
65+
use unsigned_varint::io::read_u64;
5766

5867
let size = read_u64(&mut r)?;
5968
if size > S::to_u64() || size > u8::max_value() as u64 {

src/lib.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
#![cfg_attr(not(feature = "std"), no_std)]
5454

5555
#[cfg(not(feature = "std"))]
56+
#[macro_use]
5657
extern crate alloc;
5758

5859
#[cfg(any(test, feature = "arb"))]
@@ -92,3 +93,26 @@ pub use crate::hasher_impl::sha3::{Sha3Digest, Sha3_224, Sha3_256, Sha3_384, Sha
9293
#[cfg(feature = "strobe")]
9394
pub use crate::hasher_impl::strobe::{Strobe256, Strobe512, StrobeDigest, StrobeHasher};
9495
pub use crate::hasher_impl::unknown::UnknownDigest;
96+
97+
#[cfg(feature = "std")]
98+
use std::io;
99+
#[cfg(not(feature = "std"))]
100+
use core2::io;
101+
102+
use unsigned_varint::{encode, decode};
103+
104+
/// Reader function from unsigned_varint
105+
pub fn varint_read_u64<R: io::Read>(mut r: R) -> Result<u64> {
106+
let mut b = encode::u64_buffer();
107+
for i in 0..b.len() {
108+
let n = r.read(&mut (b[i..i + 1]))?;
109+
if n == 0 {
110+
return Err(error::Error::Varint(decode::Error::Insufficient));
111+
}
112+
else if decode::is_last(b[i]) {
113+
return Ok(decode::u64(&b[..=i]).unwrap().0);
114+
}
115+
}
116+
Err(error::Error::Varint(decode::Error::Overflow))
117+
}
118+

src/multihash.rs

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
use crate::hasher::{Digest, Size};
22
use crate::Error;
3+
#[cfg(not(feature = "std"))]
4+
use alloc::vec::Vec;
35
use core::convert::TryFrom;
4-
#[cfg(feature = "std")]
6+
57
use core::convert::TryInto;
68
use core::fmt::Debug;
79
use generic_array::{ArrayLength, GenericArray};
810

11+
#[cfg(feature = "std")]
12+
use std::io;
13+
14+
#[cfg(not(feature = "std"))]
15+
use core2::io;
16+
917
/// Trait that implements hashing.
1018
///
1119
/// It is usually implemented by a custom code table enum that derives the [`Multihash` derive].
@@ -118,8 +126,7 @@ impl<S: Size> Multihash<S> {
118126
}
119127

120128
/// Reads a multihash from a byte stream.
121-
#[cfg(feature = "std")]
122-
pub fn read<R: std::io::Read>(r: R) -> Result<Self, Error>
129+
pub fn read<R: io::Read>(r: R) -> Result<Self, Error>
123130
where
124131
Self: Sized,
125132
{
@@ -131,7 +138,6 @@ impl<S: Size> Multihash<S> {
131138
///
132139
/// You need to make sure the passed in bytes have the correct length. The digest length
133140
/// needs to match the `size` value of the multihash.
134-
#[cfg(feature = "std")]
135141
pub fn from_bytes(mut bytes: &[u8]) -> Result<Self, Error>
136142
where
137143
Self: Sized,
@@ -148,15 +154,13 @@ impl<S: Size> Multihash<S> {
148154
}
149155

150156
/// Writes a multihash to a byte stream.
151-
#[cfg(feature = "std")]
152-
pub fn write<W: std::io::Write>(&self, w: W) -> Result<(), Error> {
157+
pub fn write<W: io::Write>(&self, w: W) -> Result<(), Error> {
153158
write_multihash(w, self.code(), self.size(), self.digest())
154159
}
155160

156161
/// Returns the bytes of a multihash.
157-
#[cfg(feature = "std")]
158162
pub fn to_bytes(&self) -> Vec<u8> {
159-
let mut bytes = Vec::with_capacity(self.size().into());
163+
let mut bytes: Vec<u8> = Vec::with_capacity(self.size().into());
160164
self.write(&mut bytes)
161165
.expect("writing to a vec should never fail");
162166
bytes
@@ -172,7 +176,6 @@ impl<S: Size> core::hash::Hash for Multihash<S> {
172176
}
173177
}
174178

175-
#[cfg(feature = "std")]
176179
impl<S: Size> From<Multihash<S>> for Vec<u8> {
177180
fn from(multihash: Multihash<S>) -> Self {
178181
multihash.to_bytes()
@@ -252,10 +255,9 @@ impl parity_scale_codec::Decode for Multihash<crate::U64> {
252255
}
253256

254257
/// Writes the multihash to a byte stream.
255-
#[cfg(feature = "std")]
256258
pub fn write_multihash<W>(mut w: W, code: u64, size: u8, digest: &[u8]) -> Result<(), Error>
257259
where
258-
W: std::io::Write,
260+
W: io::Write,
259261
{
260262
use unsigned_varint::encode as varint_encode;
261263

@@ -277,16 +279,25 @@ where
277279
/// maximum/allocated size of the digest.
278280
///
279281
/// Currently the maximum size for a digest is 255 bytes.
280-
#[cfg(feature = "std")]
281282
pub fn read_multihash<R, S>(mut r: R) -> Result<(u64, u8, GenericArray<u8, S>), Error>
282283
where
283-
R: std::io::Read,
284+
R: io::Read,
284285
S: Size,
285286
{
286-
use unsigned_varint::io::read_u64;
287-
288-
let code = read_u64(&mut r)?;
289-
let size = read_u64(&mut r)?;
287+
#[cfg(not(feature = "std"))]
288+
use crate::varint_read_u64 as read_u64;
289+
290+
#[cfg(feature = "std")]
291+
use unsigned_varint::io::read_u64;
292+
293+
let code = match read_u64(&mut r) {
294+
Ok(c) => c,
295+
Err(e) => return Err(e.into()),
296+
};
297+
let size = match read_u64(&mut r) {
298+
Ok(s) => s,
299+
Err(e) => return Err(e.into()),
300+
};
290301

291302
if size > S::to_u64() || size > u8::MAX as u64 {
292303
return Err(Error::InvalidSize(size));

0 commit comments

Comments
 (0)