From 8f5d7aeb32c43f95bf2d71b862b60f8002269324 Mon Sep 17 00:00:00 2001 From: Georges Palauqui Date: Sun, 20 Oct 2024 09:21:36 +0200 Subject: [PATCH] binary_codec_sv2: refactor to std logic --- .../binary-sv2/no-serde-sv2/codec/Cargo.toml | 3 +- .../no-serde-sv2/codec/src/codec/decodable.rs | 10 ++-- .../no-serde-sv2/codec/src/codec/encodable.rs | 10 ++-- .../no-serde-sv2/codec/src/codec/mod.rs | 2 + .../codec/src/datatypes/copy_data_types.rs | 12 +++-- .../no-serde-sv2/codec/src/datatypes/mod.rs | 10 ++-- .../datatypes/non_copy_data_types/inner.rs | 12 ++--- .../src/datatypes/non_copy_data_types/mod.rs | 3 +- .../non_copy_data_types/seq_inner.rs | 4 +- .../binary-sv2/no-serde-sv2/codec/src/lib.rs | 50 +++++++++++-------- 10 files changed, 62 insertions(+), 54 deletions(-) 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 459ccd2a1..8f638bd8c 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 45389b73a..ffb6cb8a2 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,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 @@ -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 { let mut data = Vec::new(); reader.read_to_end(&mut data)?; @@ -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, Error> { match self { @@ -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, 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 70be94ecb..3040ee945 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,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; - #[cfg(not(feature = "no_std"))] + #[cfg(feature = "std")] #[allow(clippy::wrong_self_convention)] fn to_writer(self, dst: &mut impl Write) -> Result<(), E>; } @@ -26,7 +26,7 @@ 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> { let encoded_field = self.into(); @@ -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), @@ -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), 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 fd626bc8f..8902adf63 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 @@ -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; 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 be6edeadc..f842c8444 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,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 @@ -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 { let mut dst = [0_u8; Self::SIZE]; reader.read_exact(&mut dst)?; @@ -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]), @@ -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 { let mut dst = [0_u8; Self::SIZE]; reader.read_exact(&mut dst)?; @@ -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) 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 ad1bce76c..2ebee7fef 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,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 { fn from_bytes_(data: &'a mut [u8]) -> Result { Self::size_hint(data, 0)?; @@ -29,7 +29,7 @@ pub trait Sv2DataType<'a>: Sized + SizeHint + GetSize + TryInto { fn from_vec_unchecked(data: Vec) -> Self; - #[cfg(not(feature = "no_std"))] + #[cfg(feature = "std")] fn from_reader_(reader: &mut impl Read) -> Result; fn to_slice(&'a self, dst: &mut [u8]) -> Result { @@ -43,6 +43,6 @@ pub trait Sv2DataType<'a>: Sized + SizeHint + GetSize + TryInto { 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>; } 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 1703c5d2e..fa2f46693 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,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)] @@ -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 { if ISFIXED { Ok(SIZE) @@ -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 { let size = Self::expected_length_for_reader(&mut reader)?; @@ -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) => { 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 9a117786e..8bda3863f 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,3 +1,4 @@ +use alloc::string::String; #[cfg(feature = "prop_test")] use quickcheck::{Arbitrary, Gen}; @@ -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 { @@ -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 { 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 acc50f15f..f3059064a 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,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 @@ -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 { let mut header = vec![0; Self::HEADERSIZE]; reader.read_exact(&mut header)?; 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 929fb07a0..2fa4d89ba 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,11 +19,20 @@ //! Seq0255 <-> SEQ0_255[T] //! Seq064K <-> SEQ0_64K[T] //! ``` -#[cfg(not(feature = "no_std"))] + +#![cfg_attr(not(feature = "std"), no_std)] + +#[cfg(feature = "std")] use std::io::{Error as E, ErrorKind}; +#[macro_use] +extern crate alloc; + mod codec; mod datatypes; + +use alloc::vec::Vec; + pub use datatypes::{ PubKey, Seq0255, Seq064K, ShortTxId, Signature, Str0255, Sv2DataType, Sv2Option, U32AsRef, B016M, B0255, B032, B064K, U24, U256, @@ -61,9 +70,6 @@ pub mod encodable { pub use crate::codec::encodable::{Encodable, EncodableField, EncodablePrimitive}; } -#[macro_use] -extern crate alloc; - #[derive(Debug, PartialEq, Eq, Clone)] pub enum Error { OutOfBound, @@ -83,9 +89,9 @@ pub enum Error { PrimitiveConversionError, DecodableConversionError, UnInitializedDecoder, - #[cfg(not(feature = "no_std"))] + #[cfg(feature = "std")] IoError(E), - #[cfg(feature = "no_std")] + #[cfg(not(feature = "std"))] IoError, ReadError(usize, usize), VoidFieldMarker, @@ -100,7 +106,7 @@ pub enum Error { Sv2OptionHaveMoreThenOneElement(u8), } -#[cfg(not(feature = "no_std"))] +#[cfg(feature = "std")] impl From for Error { fn from(v: E) -> Self { match v.kind() { @@ -131,9 +137,9 @@ pub enum CError { PrimitiveConversionError, DecodableConversionError, UnInitializedDecoder, - #[cfg(not(feature = "no_std"))] + #[cfg(feature = "std")] IoError(E), - #[cfg(feature = "no_std")] + #[cfg(not(feature = "std"))] IoError, ReadError(usize, usize), VoidFieldMarker, @@ -166,9 +172,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 +210,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 +285,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 +301,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 +309,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 +362,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 +371,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 +393,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 +413,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 +427,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 +442,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,