diff --git a/protocols/v2/binary-sv2/no-serde-sv2/codec/Cargo.toml b/protocols/v2/binary-sv2/no-serde-sv2/codec/Cargo.toml index 459ccd2a14..8f638bd8c5 100644 --- a/protocols/v2/binary-sv2/no-serde-sv2/codec/Cargo.toml +++ b/protocols/v2/binary-sv2/no-serde-sv2/codec/Cargo.toml @@ -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"] diff --git a/protocols/v2/binary-sv2/no-serde-sv2/codec/src/codec/decodable.rs b/protocols/v2/binary-sv2/no-serde-sv2/codec/src/codec/decodable.rs index 45389b73a4..15683ae89e 100644 --- a/protocols/v2/binary-sv2/no-serde-sv2/codec/src/codec/decodable.rs +++ b/protocols/v2/binary-sv2/no-serde-sv2/codec/src/codec/decodable.rs @@ -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. @@ -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 { + #[cfg(feature = "std")] + fn from_reader(reader: &mut impl std::io::Read) -> Result { 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); for field in structure { fields.push(field.from_reader(&mut reader)?); @@ -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, Error> { + fn from_reader<'a>( + &self, + reader: &mut impl std::io::Read, + ) -> Result, Error> { match self { Self::U8 => Ok(DecodablePrimitive::U8(u8::from_reader_(reader)?)), Self::U16 => Ok(DecodablePrimitive::U16(u16::from_reader_(reader)?)), @@ -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, Error> { match self { Self::Primitive(p) => Ok(DecodableField::Primitive(p.from_reader(reader)?)), diff --git a/protocols/v2/binary-sv2/no-serde-sv2/codec/src/codec/encodable.rs b/protocols/v2/binary-sv2/no-serde-sv2/codec/src/codec/encodable.rs index 70be94ecbd..493c72b6a4 100644 --- a/protocols/v2/binary-sv2/no-serde-sv2/codec/src/codec/encodable.rs +++ b/protocols/v2/binary-sv2/no-serde-sv2/codec/src/codec/encodable.rs @@ -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; - #[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>; } // @@ -26,9 +24,9 @@ impl<'a, T: Into>> 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> { let encoded_field = self.into(); encoded_field.to_writer(dst) } @@ -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> { match self { Self::U8(v) => v.to_writer_(writer), Self::OwnedU8(v) => v.to_writer_(writer), @@ -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> { match self { Self::Primitive(p) => p.write(writer), Self::Struct(ps) => { diff --git a/protocols/v2/binary-sv2/no-serde-sv2/codec/src/codec/mod.rs b/protocols/v2/binary-sv2/no-serde-sv2/codec/src/codec/mod.rs index fd626bc8fe..a26ae8e72f 100644 --- a/protocols/v2/binary-sv2/no-serde-sv2/codec/src/codec/mod.rs +++ b/protocols/v2/binary-sv2/no-serde-sv2/codec/src/codec/mod.rs @@ -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; diff --git a/protocols/v2/binary-sv2/no-serde-sv2/codec/src/datatypes/copy_data_types.rs b/protocols/v2/binary-sv2/no-serde-sv2/codec/src/datatypes/copy_data_types.rs index be6edeadc8..c0183f1022 100644 --- a/protocols/v2/binary-sv2/no-serde-sv2/codec/src/datatypes/copy_data_types.rs +++ b/protocols/v2/binary-sv2/no-serde-sv2/codec/src/datatypes/copy_data_types.rs @@ -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 @@ -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 { + #[cfg(feature = "std")] + fn from_reader_(reader: &mut impl std::io::Read) -> Result { let mut dst = [0_u8; Self::SIZE]; reader.read_exact(&mut dst)?; Self::from_bytes_(&mut dst) @@ -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> { match self { true => writer.write_all(&[1]), false => writer.write_all(&[0]), @@ -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 { + #[cfg(feature = "std")] + fn from_reader_(reader: &mut impl std::io::Read) -> Result { let mut dst = [0_u8; Self::SIZE]; reader.read_exact(&mut dst)?; Ok(Self::from_bytes_unchecked(&mut dst)) @@ -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) } diff --git a/protocols/v2/binary-sv2/no-serde-sv2/codec/src/datatypes/mod.rs b/protocols/v2/binary-sv2/no-serde-sv2/codec/src/datatypes/mod.rs index ad1bce76c3..144bbda661 100644 --- a/protocols/v2/binary-sv2/no-serde-sv2/codec/src/datatypes/mod.rs +++ b/protocols/v2/binary-sv2/no-serde-sv2/codec/src/datatypes/mod.rs @@ -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 { fn from_bytes_(data: &'a mut [u8]) -> Result { @@ -29,8 +27,8 @@ pub trait Sv2DataType<'a>: Sized + SizeHint + GetSize + TryInto { fn from_vec_unchecked(data: Vec) -> Self; - #[cfg(not(feature = "no_std"))] - fn from_reader_(reader: &mut impl Read) -> Result; + #[cfg(feature = "std")] + fn from_reader_(reader: &mut impl std::io::Read) -> Result; fn to_slice(&'a self, dst: &mut [u8]) -> Result { if dst.len() >= self.get_size() { @@ -43,6 +41,6 @@ pub trait Sv2DataType<'a>: Sized + SizeHint + GetSize + TryInto { 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>; } diff --git a/protocols/v2/binary-sv2/no-serde-sv2/codec/src/datatypes/non_copy_data_types/inner.rs b/protocols/v2/binary-sv2/no-serde-sv2/codec/src/datatypes/non_copy_data_types/inner.rs index 1703c5d2ea..5386f33c30 100644 --- a/protocols/v2/binary-sv2/no-serde-sv2/codec/src/datatypes/non_copy_data_types/inner.rs +++ b/protocols/v2/binary-sv2/no-serde-sv2/codec/src/datatypes/non_copy_data_types/inner.rs @@ -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)] @@ -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 { + #[cfg(feature = "std")] + fn expected_length_for_reader(mut reader: impl std::io::Read) -> Result { if ISFIXED { Ok(SIZE) } else { @@ -274,8 +272,8 @@ where Self::Owned(data) } - #[cfg(not(feature = "no_std"))] - fn from_reader_(mut reader: &mut impl Read) -> Result { + #[cfg(feature = "std")] + fn from_reader_(mut reader: &mut impl std::io::Read) -> Result { let size = Self::expected_length_for_reader(&mut reader)?; let mut dst = vec![0; size]; @@ -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> { match self { Inner::Ref(data) => { writer.write_all(data)?; diff --git a/protocols/v2/binary-sv2/no-serde-sv2/codec/src/datatypes/non_copy_data_types/mod.rs b/protocols/v2/binary-sv2/no-serde-sv2/codec/src/datatypes/non_copy_data_types/mod.rs index 9a117786e2..40c47a7ba4 100644 --- a/protocols/v2/binary-sv2/no-serde-sv2/codec/src/datatypes/non_copy_data_types/mod.rs +++ b/protocols/v2/binary-sv2/no-serde-sv2/codec/src/datatypes/non_copy_data_types/mod.rs @@ -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; @@ -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 { @@ -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 { diff --git a/protocols/v2/binary-sv2/no-serde-sv2/codec/src/datatypes/non_copy_data_types/seq_inner.rs b/protocols/v2/binary-sv2/no-serde-sv2/codec/src/datatypes/non_copy_data_types/seq_inner.rs index acc50f15f4..edecd6c0f8 100644 --- a/protocols/v2/binary-sv2/no-serde-sv2/codec/src/datatypes/non_copy_data_types/seq_inner.rs +++ b/protocols/v2/binary-sv2/no-serde-sv2/codec/src/datatypes/non_copy_data_types/seq_inner.rs @@ -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)] @@ -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 { + #[cfg(feature = "std")] + fn from_reader(reader: &mut impl std::io::Read) -> Result { let mut header = vec![0; Self::HEADERSIZE]; reader.read_exact(&mut header)?; @@ -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> for Vec { +impl<'a, T> core::convert::TryFrom> for Vec { type Error = &'static str; fn try_from(v: Seq0255<'a, T>) -> Result { if v.0.len() > 255 { @@ -292,7 +289,7 @@ impl<'a, T> std::convert::TryFrom> for Vec { } #[cfg(feature = "prop_test")] -impl<'a, T> std::convert::TryFrom> for Vec { +impl<'a, T> core::convert::TryFrom> for Vec { type Error = &'static str; fn try_from(v: Seq064K<'a, T>) -> Result { if v.0.len() > 64 { diff --git a/protocols/v2/binary-sv2/no-serde-sv2/codec/src/lib.rs b/protocols/v2/binary-sv2/no-serde-sv2/codec/src/lib.rs index 929fb07a0e..ae3e51adcc 100644 --- a/protocols/v2/binary-sv2/no-serde-sv2/codec/src/lib.rs +++ b/protocols/v2/binary-sv2/no-serde-sv2/codec/src/lib.rs @@ -19,8 +19,8 @@ //! Seq0255 <-> SEQ0_255[T] //! Seq064K <-> SEQ0_64K[T] //! ``` -#[cfg(not(feature = "no_std"))] -use std::io::{Error as E, ErrorKind}; + +#![cfg_attr(not(feature = "std"), no_std)] mod codec; mod datatypes; @@ -35,6 +35,8 @@ pub use crate::codec::{ Fixed, GetSize, SizeHint, }; +use alloc::vec::Vec; + #[allow(clippy::wrong_self_convention)] pub fn to_bytes(src: T) -> Result, Error> { let mut result = vec![0_u8; src.get_size()]; @@ -83,9 +85,9 @@ pub enum Error { PrimitiveConversionError, DecodableConversionError, UnInitializedDecoder, - #[cfg(not(feature = "no_std"))] - IoError(E), - #[cfg(feature = "no_std")] + #[cfg(feature = "std")] + IoError(std::io::Error), + #[cfg(not(feature = "std"))] IoError, ReadError(usize, usize), VoidFieldMarker, @@ -100,11 +102,11 @@ pub enum Error { Sv2OptionHaveMoreThenOneElement(u8), } -#[cfg(not(feature = "no_std"))] -impl From for Error { - fn from(v: E) -> Self { +#[cfg(feature = "std")] +impl From for Error { + fn from(v: std::io::Error) -> Self { match v.kind() { - ErrorKind::UnexpectedEof => Error::OutOfBound, + std::io::ErrorKind::UnexpectedEof => Error::OutOfBound, _ => Error::IoError(v), } } @@ -131,9 +133,9 @@ pub enum CError { PrimitiveConversionError, DecodableConversionError, UnInitializedDecoder, - #[cfg(not(feature = "no_std"))] - IoError(E), - #[cfg(feature = "no_std")] + #[cfg(feature = "std")] + IoError(std::io::Error), + #[cfg(not(feature = "std"))] IoError, ReadError(usize, usize), VoidFieldMarker, @@ -166,9 +168,9 @@ impl From for CError { Error::PrimitiveConversionError => CError::PrimitiveConversionError, Error::DecodableConversionError => CError::DecodableConversionError, Error::UnInitializedDecoder => CError::UnInitializedDecoder, - #[cfg(not(feature = "no_std"))] + #[cfg(feature = "std")] Error::IoError(e) => CError::IoError(e), - #[cfg(feature = "no_std")] + #[cfg(not(feature = "std"))] Error::IoError => CError::IoError, Error::ReadError(u1, u2) => CError::ReadError(u1, u2), Error::VoidFieldMarker => CError::VoidFieldMarker, @@ -204,9 +206,9 @@ impl Drop for CError { Self::PrimitiveConversionError => (), Self::DecodableConversionError => (), Self::UnInitializedDecoder => (), - #[cfg(not(feature = "no_std"))] + #[cfg(feature = "std")] Self::IoError(_) => (), - #[cfg(feature = "no_std")] + #[cfg(not(feature = "std"))] Self::IoError => (), Self::ReadError(_, _) => (), Self::VoidFieldMarker => (), @@ -279,7 +281,7 @@ impl From<&[u8]> for CVec { // Get the length, first, then the pointer (doing it the other way around **currently** doesn't cause UB, but it may be unsound due to unclear (to me, at least) guarantees of the std lib) let len = buffer.len(); let ptr = buffer.as_mut_ptr(); - std::mem::forget(buffer); + core::mem::forget(buffer); CVec { data: ptr, @@ -295,7 +297,7 @@ impl From<&[u8]> for CVec { /// #[no_mangle] pub unsafe extern "C" fn cvec_from_buffer(data: *const u8, len: usize) -> CVec { - let input = std::slice::from_raw_parts(data, len); + let input = core::slice::from_raw_parts(data, len); let mut buffer: Vec = vec![0; len]; buffer.copy_from_slice(input); @@ -303,7 +305,7 @@ pub unsafe extern "C" fn cvec_from_buffer(data: *const u8, len: usize) -> CVec { // Get the length, first, then the pointer (doing it the other way around **currently** doesn't cause UB, but it may be unsound due to unclear (to me, at least) guarantees of the std lib) let len = buffer.len(); let ptr = buffer.as_mut_ptr(); - std::mem::forget(buffer); + core::mem::forget(buffer); CVec { data: ptr, @@ -356,7 +358,7 @@ impl<'a, const A: bool, const B: usize, const C: usize, const D: usize> let len = inner.len(); let cap = inner.capacity(); let ptr = inner.as_mut_ptr(); - std::mem::forget(inner); + core::mem::forget(inner); (ptr, len, cap) } @@ -365,7 +367,7 @@ impl<'a, const A: bool, const B: usize, const C: usize, const D: usize> let len = inner.len(); let cap = inner.capacity(); let ptr = inner.as_mut_ptr(); - std::mem::forget(inner); + core::mem::forget(inner); (ptr, len, cap) } @@ -387,7 +389,7 @@ pub unsafe extern "C" fn init_cvec2() -> CVec2 { // Get the length, first, then the pointer (doing it the other way around **currently** doesn't cause UB, but it may be unsound due to unclear (to me, at least) guarantees of the std lib) let len = buffer.len(); let ptr = buffer.as_mut_ptr(); - std::mem::forget(buffer); + core::mem::forget(buffer); CVec2 { data: ptr, @@ -407,7 +409,7 @@ pub unsafe extern "C" fn cvec2_push(cvec2: &mut CVec2, cvec: CVec) { let len = buffer.len(); let ptr = buffer.as_mut_ptr(); - std::mem::forget(buffer); + core::mem::forget(buffer); cvec2.data = ptr; cvec2.len = len; @@ -421,7 +423,7 @@ impl<'a, T: Into> From> for CVec2 { let len = v.len(); let capacity = v.capacity(); let data = v.as_mut_ptr(); - std::mem::forget(v); + core::mem::forget(v); Self { data, len, @@ -436,7 +438,7 @@ impl<'a, T: Into> From> for CVec2 { let len = v.len(); let capacity = v.capacity(); let data = v.as_mut_ptr(); - std::mem::forget(v); + core::mem::forget(v); Self { data, len,