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
- 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 8ef9d5e
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
Error,
};
use alloc::vec::Vec;
use std::convert::TryFrom;
use core::convert::TryFrom;
#[cfg(not(feature = "no_std"))]
use std::io::{Cursor, Read};

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,5 +1,7 @@
//! Copy data types
use crate::{codec::Fixed, datatypes::Sv2DataType, Error};

use alloc::vec::Vec;
use core::convert::{TryFrom, TryInto};

#[cfg(not(feature = "no_std"))]
Expand Down
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,
};

use alloc::vec::Vec;
use core::convert::TryInto;
#[cfg(not(feature = "no_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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use crate::{
datatypes::Sv2DataType,
Error,
};
use core::convert::TryFrom;
use std::convert::TryInto;

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

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 @@ -280,7 +280,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 +292,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
23 changes: 14 additions & 9 deletions protocols/v2/binary-sv2/no-serde-sv2/codec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
//! Seq0255 <-> SEQ0_255[T]
//! Seq064K <-> SEQ0_64K[T]
//! ```
#![cfg_attr(feature = "no_std", no_std)]

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

Expand All @@ -35,6 +38,8 @@ pub use crate::codec::{
Fixed, GetSize, SizeHint,
};

use alloc::vec::Vec;

#[allow(clippy::wrong_self_convention)]
pub fn to_bytes<T: Encodable + GetSize>(src: T) -> Result<Vec<u8>, Error> {
let mut result = vec![0_u8; src.get_size()];
Expand Down Expand Up @@ -279,7 +284,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,
Expand All @@ -295,15 +300,15 @@ 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);

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

View check run for this annotation

Codecov / codecov/patch

protocols/v2/binary-sv2/no-serde-sv2/codec/src/lib.rs#L303

Added line #L303 was not covered by tests

let mut buffer: Vec<u8> = vec![0; len];
buffer.copy_from_slice(input);

// 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,
Expand Down Expand Up @@ -356,7 +361,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);

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

View check run for this annotation

Codecov / codecov/patch

protocols/v2/binary-sv2/no-serde-sv2/codec/src/lib.rs#L364

Added line #L364 was not covered by tests

(ptr, len, cap)
}
Expand All @@ -365,7 +370,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);

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

View check run for this annotation

Codecov / codecov/patch

protocols/v2/binary-sv2/no-serde-sv2/codec/src/lib.rs#L373

Added line #L373 was not covered by tests

(ptr, len, cap)
}
Expand All @@ -387,7 +392,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,
Expand All @@ -407,7 +412,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;
Expand All @@ -421,7 +426,7 @@ impl<'a, T: Into<CVec>> From<Seq0255<'a, T>> for CVec2 {
let len = v.len();
let capacity = v.capacity();
let data = v.as_mut_ptr();
std::mem::forget(v);
core::mem::forget(v);

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

View check run for this annotation

Codecov / codecov/patch

protocols/v2/binary-sv2/no-serde-sv2/codec/src/lib.rs#L429

Added line #L429 was not covered by tests
Self {
data,
len,
Expand All @@ -436,7 +441,7 @@ impl<'a, T: Into<CVec>> From<Seq064K<'a, T>> for CVec2 {
let len = v.len();
let capacity = v.capacity();
let data = v.as_mut_ptr();
std::mem::forget(v);
core::mem::forget(v);

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

View check run for this annotation

Codecov / codecov/patch

protocols/v2/binary-sv2/no-serde-sv2/codec/src/lib.rs#L444

Added line #L444 was not covered by tests
Self {
data,
len,
Expand Down

0 comments on commit 8ef9d5e

Please sign in to comment.