Skip to content

Commit

Permalink
binary_codec_sv2: refactor to std logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Georges Palauqui committed Oct 20, 2024
1 parent c658909 commit 8f5d7ae
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 54 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
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use crate::{
Error,
};
use alloc::vec::Vec;
use std::convert::TryFrom;
#[cfg(not(feature = "no_std"))]
use core::convert::TryFrom;
#[cfg(feature = "std")]
use std::io::{Cursor, Read};

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

#[cfg(not(feature = "no_std"))]
#[cfg(feature = "std")]
fn from_reader(reader: &mut impl Read) -> Result<Self, Error> {
let mut data = Vec::new();
reader.read_to_end(&mut data)?;
Expand Down Expand Up @@ -236,7 +236,7 @@ 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> {
match self {
Expand Down Expand Up @@ -306,7 +306,7 @@ 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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ use crate::{
Error,
};
use alloc::vec::Vec;
#[cfg(not(feature = "no_std"))]
#[cfg(feature = "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>;
}
Expand All @@ -26,7 +26,7 @@ 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> {
let encoded_field = self.into();
Expand Down Expand Up @@ -76,7 +76,7 @@ impl<'a> EncodablePrimitive<'a> {
}
}

#[cfg(not(feature = "no_std"))]
#[cfg(feature = "std")]
pub fn write(&self, writer: &mut impl Write) -> Result<(), E> {
match self {
Self::U8(v) => v.to_writer_(writer),
Expand Down Expand Up @@ -145,7 +145,7 @@ impl<'a> EncodableField<'a> {
}
}

#[cfg(not(feature = "no_std"))]
#[cfg(feature = "std")]
pub fn to_writer(&self, writer: &mut impl Write) -> Result<(), E> {
match self {
Self::Primitive(p) => p.write(writer),
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 @@ -8,6 +8,8 @@ use crate::Error;
pub mod decodable;
pub mod encodable;
mod impls;

use alloc::vec::Vec;
#[cfg(feature = "with_buffer_pool")]
use buffer_sv2::Slice;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
//! Copy data types
use crate::{codec::Fixed, datatypes::Sv2DataType, Error};
use alloc::vec::Vec;
use core::convert::{TryFrom, TryInto};

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

// Impl bool as a primitive
Expand Down Expand Up @@ -44,7 +46,7 @@ impl<'a> Sv2DataType<'a> for bool {
Self::from_bytes_unchecked(&mut data)
}

#[cfg(not(feature = "no_std"))]
#[cfg(feature = "std")]
fn from_reader_(reader: &mut impl Read) -> Result<Self, Error> {
let mut dst = [0_u8; Self::SIZE];
reader.read_exact(&mut dst)?;
Expand All @@ -58,7 +60,7 @@ impl<'a> Sv2DataType<'a> for bool {
};
}

#[cfg(not(feature = "no_std"))]
#[cfg(feature = "std")]
fn to_writer_(&self, writer: &mut impl Write) -> Result<(), E> {
match self {
true => writer.write_all(&[1]),
Expand Down Expand Up @@ -104,7 +106,7 @@ macro_rules! impl_sv2_for_unsigned {
Self::from_bytes_unchecked(&mut data)
}

#[cfg(not(feature = "no_std"))]
#[cfg(feature = "std")]
fn from_reader_(reader: &mut impl Read) -> Result<Self, Error> {
let mut dst = [0_u8; Self::SIZE];
reader.read_exact(&mut dst)?;
Expand All @@ -117,7 +119,7 @@ macro_rules! impl_sv2_for_unsigned {
dst.copy_from_slice(&src);
}

#[cfg(not(feature = "no_std"))]
#[cfg(feature = "std")]
fn to_writer_(&self, writer: &mut impl Write) -> Result<(), E> {
let bytes = self.to_le_bytes();
writer.write_all(&bytes)
Expand Down
10 changes: 5 additions & 5 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,11 +12,11 @@ pub use non_copy_data_types::{
B0255, B032, B064K, U256,
};

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

use std::convert::TryInto;

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

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

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

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

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

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

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

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

#[cfg(not(feature = "no_std"))]
#[cfg(feature = "std")]
fn expected_length_for_reader(mut reader: impl Read) -> Result<usize, Error> {
if ISFIXED {
Ok(SIZE)
Expand Down Expand Up @@ -274,7 +274,7 @@ where
Self::Owned(data)
}

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

Expand All @@ -300,7 +300,7 @@ where
}
}

#[cfg(not(feature = "no_std"))]
#[cfg(feature = "std")]
fn to_writer_(&self, writer: &mut impl Write) -> Result<(), E> {
match self {
Inner::Ref(data) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use alloc::string::String;
#[cfg(feature = "prop_test")]
use quickcheck::{Arbitrary, Gen};

Expand Down Expand Up @@ -28,7 +29,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 +40,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,7 +52,7 @@ impl<'a, const SIZE: usize> Seq064K<'a, super::inner::Inner<'a, true, SIZE, 0, 0
}
}

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

/// The liftime is here only for type compatibility with serde-sv2
Expand Down Expand Up @@ -192,7 +192,7 @@ macro_rules! impl_codec_for_sequence {
Ok(Self(inner, PhantomData))
}

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

0 comments on commit 8f5d7ae

Please sign in to comment.