Skip to content

Commit dfb23fe

Browse files
author
Georges Palauqui
committed
binary_codec_sv2: is a no_std crate by default
- with a `std` feature to enable some (already broken) std::io::{Cursor, Error, Read, Write} related funtionalities - std::vec::Vec -> alloc::vec::Vec - std::mem -> core::mem - std::slice -> core::slice - std::convert::{TryFrom, TryInto} -> core::convert::{TryFrom, TryInto}
1 parent 45dca98 commit dfb23fe

File tree

10 files changed

+81
-85
lines changed

10 files changed

+81
-85
lines changed

protocols/v2/binary-sv2/no-serde-sv2/codec/Cargo.toml

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ quickcheck = {version = "1.0.0", optional = true}
1818
buffer_sv2 = { version = "^1.0.0", path = "../../../../../utils/buffer", optional=true}
1919

2020
[features]
21-
no_std = []
22-
default = ["no_std"]
21+
std = []
2322
prop_test = ["quickcheck"]
2423
with_buffer_pool = ["buffer_sv2"]
2524

protocols/v2/binary-sv2/no-serde-sv2/codec/src/codec/decodable.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ use crate::{
66
Error,
77
};
88
use alloc::vec::Vec;
9-
use std::convert::TryFrom;
10-
#[cfg(not(feature = "no_std"))]
11-
use std::io::{Cursor, Read};
9+
use core::convert::TryFrom;
1210

1311
/// Implmented by all the decodable structure, it can be derived for every structure composed only
1412
/// by primitives or other Decodable.
@@ -34,15 +32,15 @@ pub trait Decodable<'a>: Sized {
3432
Self::from_decoded_fields(fields)
3533
}
3634

37-
#[cfg(not(feature = "no_std"))]
38-
fn from_reader(reader: &mut impl Read) -> Result<Self, Error> {
35+
#[cfg(feature = "std")]
36+
fn from_reader(reader: &mut impl std::io::Read) -> Result<Self, Error> {
3937
let mut data = Vec::new();
4038
reader.read_to_end(&mut data)?;
4139

4240
let structure = Self::get_structure(&data[..])?;
4341

4442
let mut fields = Vec::new();
45-
let mut reader = Cursor::new(data);
43+
let mut reader = std::io::Cursor::new(data);
4644

4745
for field in structure {
4846
fields.push(field.from_reader(&mut reader)?);
@@ -236,9 +234,12 @@ impl PrimitiveMarker {
236234
}
237235

238236
#[allow(clippy::wrong_self_convention)]
239-
#[cfg(not(feature = "no_std"))]
237+
#[cfg(feature = "std")]
240238
#[allow(clippy::wrong_self_convention)]
241-
fn from_reader<'a>(&self, reader: &mut impl Read) -> Result<DecodablePrimitive<'a>, Error> {
239+
fn from_reader<'a>(
240+
&self,
241+
reader: &mut impl std::io::Read,
242+
) -> Result<DecodablePrimitive<'a>, Error> {
242243
match self {
243244
Self::U8 => Ok(DecodablePrimitive::U8(u8::from_reader_(reader)?)),
244245
Self::U16 => Ok(DecodablePrimitive::U16(u16::from_reader_(reader)?)),
@@ -306,11 +307,11 @@ impl FieldMarker {
306307
}
307308

308309
#[allow(clippy::wrong_self_convention)]
309-
#[cfg(not(feature = "no_std"))]
310+
#[cfg(feature = "std")]
310311
#[allow(clippy::wrong_self_convention)]
311312
pub(crate) fn from_reader<'a>(
312313
&self,
313-
reader: &mut impl Read,
314+
reader: &mut impl std::io::Read,
314315
) -> Result<DecodableField<'a>, Error> {
315316
match self {
316317
Self::Primitive(p) => Ok(DecodableField::Primitive(p.from_reader(reader)?)),

protocols/v2/binary-sv2/no-serde-sv2/codec/src/codec/encodable.rs

+8-10
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,14 @@ use crate::{
66
Error,
77
};
88
use alloc::vec::Vec;
9-
#[cfg(not(feature = "no_std"))]
10-
use std::io::{Error as E, Write};
119

1210
pub trait Encodable {
1311
#[allow(clippy::wrong_self_convention)]
1412
fn to_bytes(self, dst: &mut [u8]) -> Result<usize, Error>;
1513

16-
#[cfg(not(feature = "no_std"))]
14+
#[cfg(feature = "std")]
1715
#[allow(clippy::wrong_self_convention)]
18-
fn to_writer(self, dst: &mut impl Write) -> Result<(), E>;
16+
fn to_writer(self, dst: &mut impl std::io::Write) -> Result<(), std::io::Error>;
1917
}
2018

2119
//
@@ -26,9 +24,9 @@ impl<'a, T: Into<EncodableField<'a>>> Encodable for T {
2624
encoded_field.encode(dst, 0)
2725
}
2826

29-
#[cfg(not(feature = "no_std"))]
27+
#[cfg(feature = "std")]
3028
#[allow(clippy::wrong_self_convention, unconditional_recursion)]
31-
fn to_writer(self, dst: &mut impl Write) -> Result<(), E> {
29+
fn to_writer(self, dst: &mut impl std::io::Write) -> Result<(), std::io::Error> {
3230
let encoded_field = self.into();
3331
encoded_field.to_writer(dst)
3432
}
@@ -76,8 +74,8 @@ impl<'a> EncodablePrimitive<'a> {
7674
}
7775
}
7876

79-
#[cfg(not(feature = "no_std"))]
80-
pub fn write(&self, writer: &mut impl Write) -> Result<(), E> {
77+
#[cfg(feature = "std")]
78+
pub fn write(&self, writer: &mut impl std::io::Write) -> Result<(), std::io::Error> {
8179
match self {
8280
Self::U8(v) => v.to_writer_(writer),
8381
Self::OwnedU8(v) => v.to_writer_(writer),
@@ -145,8 +143,8 @@ impl<'a> EncodableField<'a> {
145143
}
146144
}
147145

148-
#[cfg(not(feature = "no_std"))]
149-
pub fn to_writer(&self, writer: &mut impl Write) -> Result<(), E> {
146+
#[cfg(feature = "std")]
147+
pub fn to_writer(&self, writer: &mut impl std::io::Write) -> Result<(), std::io::Error> {
150148
match self {
151149
Self::Primitive(p) => p.write(writer),
152150
Self::Struct(ps) => {

protocols/v2/binary-sv2/no-serde-sv2/codec/src/codec/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ mod impls;
1111
#[cfg(feature = "with_buffer_pool")]
1212
use buffer_sv2::Slice;
1313

14+
use alloc::vec::Vec;
15+
1416
/// Return the encoded byte size or a `Decodable`
1517
pub trait SizeHint {
1618
fn size_hint(data: &[u8], offset: usize) -> Result<usize, Error>;

protocols/v2/binary-sv2/no-serde-sv2/codec/src/datatypes/copy_data_types.rs

+10-11
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
//! Copy data types
22
use crate::{codec::Fixed, datatypes::Sv2DataType, Error};
3-
use core::convert::{TryFrom, TryInto};
43

5-
#[cfg(not(feature = "no_std"))]
6-
use std::io::{Error as E, Read, Write};
4+
use alloc::vec::Vec;
5+
use core::convert::{TryFrom, TryInto};
76

87
// Impl bool as a primitive
98

@@ -44,8 +43,8 @@ impl<'a> Sv2DataType<'a> for bool {
4443
Self::from_bytes_unchecked(&mut data)
4544
}
4645

47-
#[cfg(not(feature = "no_std"))]
48-
fn from_reader_(reader: &mut impl Read) -> Result<Self, Error> {
46+
#[cfg(feature = "std")]
47+
fn from_reader_(reader: &mut impl std::io::Read) -> Result<Self, Error> {
4948
let mut dst = [0_u8; Self::SIZE];
5049
reader.read_exact(&mut dst)?;
5150
Self::from_bytes_(&mut dst)
@@ -58,8 +57,8 @@ impl<'a> Sv2DataType<'a> for bool {
5857
};
5958
}
6059

61-
#[cfg(not(feature = "no_std"))]
62-
fn to_writer_(&self, writer: &mut impl Write) -> Result<(), E> {
60+
#[cfg(feature = "std")]
61+
fn to_writer_(&self, writer: &mut impl std::io::Write) -> Result<(), std::io::Error> {
6362
match self {
6463
true => writer.write_all(&[1]),
6564
false => writer.write_all(&[0]),
@@ -104,8 +103,8 @@ macro_rules! impl_sv2_for_unsigned {
104103
Self::from_bytes_unchecked(&mut data)
105104
}
106105

107-
#[cfg(not(feature = "no_std"))]
108-
fn from_reader_(reader: &mut impl Read) -> Result<Self, Error> {
106+
#[cfg(feature = "std")]
107+
fn from_reader_(reader: &mut impl std::io::Read) -> Result<Self, Error> {
109108
let mut dst = [0_u8; Self::SIZE];
110109
reader.read_exact(&mut dst)?;
111110
Ok(Self::from_bytes_unchecked(&mut dst))
@@ -117,8 +116,8 @@ macro_rules! impl_sv2_for_unsigned {
117116
dst.copy_from_slice(&src);
118117
}
119118

120-
#[cfg(not(feature = "no_std"))]
121-
fn to_writer_(&self, writer: &mut impl Write) -> Result<(), E> {
119+
#[cfg(feature = "std")]
120+
fn to_writer_(&self, writer: &mut impl std::io::Write) -> Result<(), std::io::Error> {
122121
let bytes = self.to_le_bytes();
123122
writer.write_all(&bytes)
124123
}

protocols/v2/binary-sv2/no-serde-sv2/codec/src/datatypes/mod.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@ pub use non_copy_data_types::{
1212
B0255, B032, B064K, U256,
1313
};
1414

15-
#[cfg(not(feature = "no_std"))]
16-
use std::io::{Error as E, Read, Write};
17-
18-
use std::convert::TryInto;
15+
use alloc::vec::Vec;
16+
use core::convert::TryInto;
1917

2018
pub trait Sv2DataType<'a>: Sized + SizeHint + GetSize + TryInto<FieldMarker> {
2119
fn from_bytes_(data: &'a mut [u8]) -> Result<Self, Error> {
@@ -29,8 +27,8 @@ pub trait Sv2DataType<'a>: Sized + SizeHint + GetSize + TryInto<FieldMarker> {
2927

3028
fn from_vec_unchecked(data: Vec<u8>) -> Self;
3129

32-
#[cfg(not(feature = "no_std"))]
33-
fn from_reader_(reader: &mut impl Read) -> Result<Self, Error>;
30+
#[cfg(feature = "std")]
31+
fn from_reader_(reader: &mut impl std::io::Read) -> Result<Self, Error>;
3432

3533
fn to_slice(&'a self, dst: &mut [u8]) -> Result<usize, Error> {
3634
if dst.len() >= self.get_size() {
@@ -43,6 +41,6 @@ pub trait Sv2DataType<'a>: Sized + SizeHint + GetSize + TryInto<FieldMarker> {
4341

4442
fn to_slice_unchecked(&'a self, dst: &mut [u8]);
4543

46-
#[cfg(not(feature = "no_std"))]
47-
fn to_writer_(&self, writer: &mut impl Write) -> Result<(), E>;
44+
#[cfg(feature = "std")]
45+
fn to_writer_(&self, writer: &mut impl std::io::Write) -> Result<(), std::io::Error>;
4846
}

protocols/v2/binary-sv2/no-serde-sv2/codec/src/datatypes/non_copy_data_types/inner.rs

+8-10
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@ use crate::{
44
datatypes::Sv2DataType,
55
Error,
66
};
7-
use core::convert::TryFrom;
8-
use std::convert::TryInto;
97

10-
#[cfg(not(feature = "no_std"))]
11-
use std::io::{Error as E, Read, Write};
8+
use alloc::vec::Vec;
9+
use core::convert::{TryFrom, TryInto};
1210

1311
#[repr(C)]
1412
#[derive(Debug)]
@@ -116,8 +114,8 @@ impl<'a, const ISFIXED: bool, const SIZE: usize, const HEADERSIZE: usize, const
116114
}
117115
}
118116

119-
#[cfg(not(feature = "no_std"))]
120-
fn expected_length_for_reader(mut reader: impl Read) -> Result<usize, Error> {
117+
#[cfg(feature = "std")]
118+
fn expected_length_for_reader(mut reader: impl std::io::Read) -> Result<usize, Error> {
121119
if ISFIXED {
122120
Ok(SIZE)
123121
} else {
@@ -274,8 +272,8 @@ where
274272
Self::Owned(data)
275273
}
276274

277-
#[cfg(not(feature = "no_std"))]
278-
fn from_reader_(mut reader: &mut impl Read) -> Result<Self, Error> {
275+
#[cfg(feature = "std")]
276+
fn from_reader_(mut reader: &mut impl std::io::Read) -> Result<Self, Error> {
279277
let size = Self::expected_length_for_reader(&mut reader)?;
280278

281279
let mut dst = vec![0; size];
@@ -300,8 +298,8 @@ where
300298
}
301299
}
302300

303-
#[cfg(not(feature = "no_std"))]
304-
fn to_writer_(&self, writer: &mut impl Write) -> Result<(), E> {
301+
#[cfg(feature = "std")]
302+
fn to_writer_(&self, writer: &mut impl std::io::Write) -> Result<(), std::io::Error> {
305303
match self {
306304
Inner::Ref(data) => {
307305
writer.write_all(data)?;

protocols/v2/binary-sv2/no-serde-sv2/codec/src/datatypes/non_copy_data_types/mod.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#[cfg(feature = "prop_test")]
22
use quickcheck::{Arbitrary, Gen};
33

4+
use alloc::string::String;
5+
#[cfg(feature = "prop_test")]
6+
use alloc::vec::Vec;
7+
48
mod inner;
59
mod seq_inner;
610

@@ -28,7 +32,6 @@ impl<'decoder> From<[u8; 32]> for U256<'decoder> {
2832
}
2933
}
3034

31-
#[cfg(not(feature = "with_serde"))]
3235
#[cfg(feature = "prop_test")]
3336
impl<'a> U256<'a> {
3437
pub fn from_gen(g: &mut Gen) -> Self {
@@ -40,7 +43,6 @@ impl<'a> U256<'a> {
4043
}
4144
}
4245

43-
#[cfg(not(feature = "with_serde"))]
4446
#[cfg(feature = "prop_test")]
4547
impl<'a> B016M<'a> {
4648
pub fn from_gen(g: &mut Gen) -> Self {

protocols/v2/binary-sv2/no-serde-sv2/codec/src/datatypes/non_copy_data_types/seq_inner.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,6 @@ impl<'a, const SIZE: usize> Seq064K<'a, super::inner::Inner<'a, true, SIZE, 0, 0
5252
}
5353
}
5454

55-
#[cfg(not(feature = "no_std"))]
56-
use std::io::Read;
57-
5855
/// The liftime is here only for type compatibility with serde-sv2
5956
#[repr(C)]
6057
#[derive(Debug, Clone, Eq, PartialEq)]
@@ -192,8 +189,8 @@ macro_rules! impl_codec_for_sequence {
192189
Ok(Self(inner, PhantomData))
193190
}
194191

195-
#[cfg(not(feature = "no_std"))]
196-
fn from_reader(reader: &mut impl Read) -> Result<Self, Error> {
192+
#[cfg(feature = "std")]
193+
fn from_reader(reader: &mut impl std::io::Read) -> Result<Self, Error> {
197194
let mut header = vec![0; Self::HEADERSIZE];
198195
reader.read_exact(&mut header)?;
199196

@@ -280,7 +277,7 @@ impl_into_encodable_field_for_seq!(B064K<'a>);
280277
impl_into_encodable_field_for_seq!(B016M<'a>);
281278

282279
#[cfg(feature = "prop_test")]
283-
impl<'a, T> std::convert::TryFrom<Seq0255<'a, T>> for Vec<T> {
280+
impl<'a, T> core::convert::TryFrom<Seq0255<'a, T>> for Vec<T> {
284281
type Error = &'static str;
285282
fn try_from(v: Seq0255<'a, T>) -> Result<Self, Self::Error> {
286283
if v.0.len() > 255 {
@@ -292,7 +289,7 @@ impl<'a, T> std::convert::TryFrom<Seq0255<'a, T>> for Vec<T> {
292289
}
293290

294291
#[cfg(feature = "prop_test")]
295-
impl<'a, T> std::convert::TryFrom<Seq064K<'a, T>> for Vec<T> {
292+
impl<'a, T> core::convert::TryFrom<Seq064K<'a, T>> for Vec<T> {
296293
type Error = &'static str;
297294
fn try_from(v: Seq064K<'a, T>) -> Result<Self, Self::Error> {
298295
if v.0.len() > 64 {

0 commit comments

Comments
 (0)