Skip to content

Commit

Permalink
binary_codec_sv2: is a no_std crate by default
Browse files Browse the repository at this point in the history
- 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}
  • Loading branch information
Georges Palauqui committed Oct 28, 2024
1 parent 45dca98 commit dfb23fe
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 85 deletions.
3 changes: 1 addition & 2 deletions protocols/v2/binary-sv2/no-serde-sv2/codec/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ quickcheck = {version = "1.0.0", optional = true}
buffer_sv2 = { version = "^1.0.0", path = "../../../../../utils/buffer", optional=true}

[features]
no_std = []
default = ["no_std"]
std = []
prop_test = ["quickcheck"]
with_buffer_pool = ["buffer_sv2"]

Expand Down
21 changes: 11 additions & 10 deletions protocols/v2/binary-sv2/no-serde-sv2/codec/src/codec/decodable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ use crate::{
Error,
};
use alloc::vec::Vec;
use std::convert::TryFrom;
#[cfg(not(feature = "no_std"))]
use std::io::{Cursor, Read};
use core::convert::TryFrom;

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

#[cfg(not(feature = "no_std"))]
fn from_reader(reader: &mut impl Read) -> Result<Self, Error> {
#[cfg(feature = "std")]
fn from_reader(reader: &mut impl std::io::Read) -> Result<Self, Error> {

Check warning on line 36 in protocols/v2/binary-sv2/no-serde-sv2/codec/src/codec/decodable.rs

View check run for this annotation

Codecov / codecov/patch

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

Added line #L36 was not covered by tests
let mut data = Vec::new();
reader.read_to_end(&mut data)?;

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

let mut fields = Vec::new();
let mut reader = Cursor::new(data);
let mut reader = std::io::Cursor::new(data);

Check warning on line 43 in protocols/v2/binary-sv2/no-serde-sv2/codec/src/codec/decodable.rs

View check run for this annotation

Codecov / codecov/patch

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

Added line #L43 was not covered by tests

for field in structure {
fields.push(field.from_reader(&mut reader)?);
Expand Down Expand Up @@ -236,9 +234,12 @@ impl PrimitiveMarker {
}

#[allow(clippy::wrong_self_convention)]
#[cfg(not(feature = "no_std"))]
#[cfg(feature = "std")]
#[allow(clippy::wrong_self_convention)]
fn from_reader<'a>(&self, reader: &mut impl Read) -> Result<DecodablePrimitive<'a>, Error> {
fn from_reader<'a>(
&self,
reader: &mut impl std::io::Read,
) -> Result<DecodablePrimitive<'a>, Error> {
match self {
Self::U8 => Ok(DecodablePrimitive::U8(u8::from_reader_(reader)?)),
Self::U16 => Ok(DecodablePrimitive::U16(u16::from_reader_(reader)?)),
Expand Down Expand Up @@ -306,11 +307,11 @@ impl FieldMarker {
}

#[allow(clippy::wrong_self_convention)]
#[cfg(not(feature = "no_std"))]
#[cfg(feature = "std")]
#[allow(clippy::wrong_self_convention)]
pub(crate) fn from_reader<'a>(
&self,
reader: &mut impl Read,
reader: &mut impl std::io::Read,
) -> Result<DecodableField<'a>, Error> {
match self {
Self::Primitive(p) => Ok(DecodableField::Primitive(p.from_reader(reader)?)),
Expand Down
18 changes: 8 additions & 10 deletions protocols/v2/binary-sv2/no-serde-sv2/codec/src/codec/encodable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@ use crate::{
Error,
};
use alloc::vec::Vec;
#[cfg(not(feature = "no_std"))]
use std::io::{Error as E, Write};

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

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

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

#[cfg(not(feature = "no_std"))]
#[cfg(feature = "std")]
#[allow(clippy::wrong_self_convention, unconditional_recursion)]
fn to_writer(self, dst: &mut impl Write) -> Result<(), E> {
fn to_writer(self, dst: &mut impl std::io::Write) -> Result<(), std::io::Error> {

Check warning on line 29 in protocols/v2/binary-sv2/no-serde-sv2/codec/src/codec/encodable.rs

View check run for this annotation

Codecov / codecov/patch

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

Added line #L29 was not covered by tests
let encoded_field = self.into();
encoded_field.to_writer(dst)
}
Expand Down Expand Up @@ -76,8 +74,8 @@ impl<'a> EncodablePrimitive<'a> {
}
}

#[cfg(not(feature = "no_std"))]
pub fn write(&self, writer: &mut impl Write) -> Result<(), E> {
#[cfg(feature = "std")]
pub fn write(&self, writer: &mut impl std::io::Write) -> Result<(), std::io::Error> {

Check warning on line 78 in protocols/v2/binary-sv2/no-serde-sv2/codec/src/codec/encodable.rs

View check run for this annotation

Codecov / codecov/patch

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

Added line #L78 was not covered by tests
match self {
Self::U8(v) => v.to_writer_(writer),
Self::OwnedU8(v) => v.to_writer_(writer),
Expand Down Expand Up @@ -145,8 +143,8 @@ impl<'a> EncodableField<'a> {
}
}

#[cfg(not(feature = "no_std"))]
pub fn to_writer(&self, writer: &mut impl Write) -> Result<(), E> {
#[cfg(feature = "std")]
pub fn to_writer(&self, writer: &mut impl std::io::Write) -> Result<(), std::io::Error> {

Check warning on line 147 in protocols/v2/binary-sv2/no-serde-sv2/codec/src/codec/encodable.rs

View check run for this annotation

Codecov / codecov/patch

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

Added line #L147 was not covered by tests
match self {
Self::Primitive(p) => p.write(writer),
Self::Struct(ps) => {
Expand Down
2 changes: 2 additions & 0 deletions protocols/v2/binary-sv2/no-serde-sv2/codec/src/codec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ mod impls;
#[cfg(feature = "with_buffer_pool")]
use buffer_sv2::Slice;

use alloc::vec::Vec;

/// Return the encoded byte size or a `Decodable`
pub trait SizeHint {
fn size_hint(data: &[u8], offset: usize) -> Result<usize, Error>;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
//! Copy data types
use crate::{codec::Fixed, datatypes::Sv2DataType, Error};
use core::convert::{TryFrom, TryInto};

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

// Impl bool as a primitive

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

#[cfg(not(feature = "no_std"))]
fn from_reader_(reader: &mut impl Read) -> Result<Self, Error> {
#[cfg(feature = "std")]
fn from_reader_(reader: &mut impl std::io::Read) -> Result<Self, Error> {

Check warning on line 47 in protocols/v2/binary-sv2/no-serde-sv2/codec/src/datatypes/copy_data_types.rs

View check run for this annotation

Codecov / codecov/patch

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

Added line #L47 was not covered by tests
let mut dst = [0_u8; Self::SIZE];
reader.read_exact(&mut dst)?;
Self::from_bytes_(&mut dst)
Expand All @@ -58,8 +57,8 @@ impl<'a> Sv2DataType<'a> for bool {
};
}

#[cfg(not(feature = "no_std"))]
fn to_writer_(&self, writer: &mut impl Write) -> Result<(), E> {
#[cfg(feature = "std")]
fn to_writer_(&self, writer: &mut impl std::io::Write) -> Result<(), std::io::Error> {

Check warning on line 61 in protocols/v2/binary-sv2/no-serde-sv2/codec/src/datatypes/copy_data_types.rs

View check run for this annotation

Codecov / codecov/patch

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

Added line #L61 was not covered by tests
match self {
true => writer.write_all(&[1]),
false => writer.write_all(&[0]),
Expand Down Expand Up @@ -104,8 +103,8 @@ macro_rules! impl_sv2_for_unsigned {
Self::from_bytes_unchecked(&mut data)
}
#[cfg(not(feature = "no_std"))]
fn from_reader_(reader: &mut impl Read) -> Result<Self, Error> {
#[cfg(feature = "std")]
fn from_reader_(reader: &mut impl std::io::Read) -> Result<Self, Error> {
let mut dst = [0_u8; Self::SIZE];
reader.read_exact(&mut dst)?;
Ok(Self::from_bytes_unchecked(&mut dst))
Expand All @@ -117,8 +116,8 @@ macro_rules! impl_sv2_for_unsigned {
dst.copy_from_slice(&src);
}
#[cfg(not(feature = "no_std"))]
fn to_writer_(&self, writer: &mut impl Write) -> Result<(), E> {
#[cfg(feature = "std")]
fn to_writer_(&self, writer: &mut impl std::io::Write) -> Result<(), std::io::Error> {
let bytes = self.to_le_bytes();
writer.write_all(&bytes)
}
Expand Down
14 changes: 6 additions & 8 deletions protocols/v2/binary-sv2/no-serde-sv2/codec/src/datatypes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ pub use non_copy_data_types::{
B0255, B032, B064K, U256,
};

#[cfg(not(feature = "no_std"))]
use std::io::{Error as E, Read, Write};

use std::convert::TryInto;
use alloc::vec::Vec;
use core::convert::TryInto;

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

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

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

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

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

#[cfg(not(feature = "no_std"))]
fn to_writer_(&self, writer: &mut impl Write) -> Result<(), E>;
#[cfg(feature = "std")]
fn to_writer_(&self, writer: &mut impl std::io::Write) -> Result<(), std::io::Error>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ use crate::{
datatypes::Sv2DataType,
Error,
};
use core::convert::TryFrom;
use std::convert::TryInto;

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

#[repr(C)]
#[derive(Debug)]
Expand Down Expand Up @@ -116,8 +114,8 @@ impl<'a, const ISFIXED: bool, const SIZE: usize, const HEADERSIZE: usize, const
}
}

#[cfg(not(feature = "no_std"))]
fn expected_length_for_reader(mut reader: impl Read) -> Result<usize, Error> {
#[cfg(feature = "std")]
fn expected_length_for_reader(mut reader: impl std::io::Read) -> Result<usize, Error> {

Check warning on line 118 in protocols/v2/binary-sv2/no-serde-sv2/codec/src/datatypes/non_copy_data_types/inner.rs

View check run for this annotation

Codecov / codecov/patch

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

Added line #L118 was not covered by tests
if ISFIXED {
Ok(SIZE)
} else {
Expand Down Expand Up @@ -274,8 +272,8 @@ where
Self::Owned(data)
}

#[cfg(not(feature = "no_std"))]
fn from_reader_(mut reader: &mut impl Read) -> Result<Self, Error> {
#[cfg(feature = "std")]
fn from_reader_(mut reader: &mut impl std::io::Read) -> Result<Self, Error> {

Check warning on line 276 in protocols/v2/binary-sv2/no-serde-sv2/codec/src/datatypes/non_copy_data_types/inner.rs

View check run for this annotation

Codecov / codecov/patch

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

Added line #L276 was not covered by tests
let size = Self::expected_length_for_reader(&mut reader)?;

let mut dst = vec![0; size];
Expand All @@ -300,8 +298,8 @@ where
}
}

#[cfg(not(feature = "no_std"))]
fn to_writer_(&self, writer: &mut impl Write) -> Result<(), E> {
#[cfg(feature = "std")]
fn to_writer_(&self, writer: &mut impl std::io::Write) -> Result<(), std::io::Error> {

Check warning on line 302 in protocols/v2/binary-sv2/no-serde-sv2/codec/src/datatypes/non_copy_data_types/inner.rs

View check run for this annotation

Codecov / codecov/patch

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

Added line #L302 was not covered by tests
match self {
Inner::Ref(data) => {
writer.write_all(data)?;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#[cfg(feature = "prop_test")]
use quickcheck::{Arbitrary, Gen};

use alloc::string::String;
#[cfg(feature = "prop_test")]
use alloc::vec::Vec;

mod inner;
mod seq_inner;

Expand Down Expand Up @@ -28,7 +32,6 @@ impl<'decoder> From<[u8; 32]> for U256<'decoder> {
}
}

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

#[cfg(not(feature = "with_serde"))]
#[cfg(feature = "prop_test")]
impl<'a> B016M<'a> {
pub fn from_gen(g: &mut Gen) -> Self {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,6 @@ impl<'a, const SIZE: usize> Seq064K<'a, super::inner::Inner<'a, true, SIZE, 0, 0
}
}

#[cfg(not(feature = "no_std"))]
use std::io::Read;

/// The liftime is here only for type compatibility with serde-sv2
#[repr(C)]
#[derive(Debug, Clone, Eq, PartialEq)]
Expand Down Expand Up @@ -192,8 +189,8 @@ macro_rules! impl_codec_for_sequence {
Ok(Self(inner, PhantomData))
}

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

Expand Down Expand Up @@ -280,7 +277,7 @@ impl_into_encodable_field_for_seq!(B064K<'a>);
impl_into_encodable_field_for_seq!(B016M<'a>);

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

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

0 comments on commit dfb23fe

Please sign in to comment.