Skip to content

Commit

Permalink
Small update to Bytes/BytesImpl/Program conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
Rigidity committed May 18, 2024
1 parent 88d1e07 commit 2d9f5d9
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions crates/chia-protocol/src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ impl Bytes {
self.0.as_slice()
}

pub fn to_vec(&self) -> Vec<u8> {
self.0.clone()
}

pub fn into_inner(self) -> Vec<u8> {
self.0
}
Expand Down Expand Up @@ -124,6 +128,12 @@ impl From<&[u8]> for Bytes {
}
}

impl<const N: usize> From<BytesImpl<N>> for Bytes {
fn from(value: BytesImpl<N>) -> Self {
Self(value.0.to_vec())
}
}

impl From<Vec<u8>> for Bytes {
fn from(value: Vec<u8>) -> Self {
Self(value)
Expand Down Expand Up @@ -159,6 +169,14 @@ impl<const N: usize> BytesImpl<N> {
Self(bytes)
}

pub const fn len(&self) -> usize {
N
}

pub const fn is_empty(&self) -> bool {
N == 0
}

pub fn as_slice(&self) -> &[u8] {
&self.0
}
Expand Down Expand Up @@ -281,6 +299,22 @@ impl<const N: usize> TryFrom<&Vec<u8>> for BytesImpl<N> {
}
}

impl<const N: usize> TryFrom<Bytes> for BytesImpl<N> {
type Error = TryFromSliceError;

fn try_from(value: Bytes) -> Result<Self, TryFromSliceError> {
value.0.as_slice().try_into()
}
}

impl<const N: usize> TryFrom<&Bytes> for BytesImpl<N> {
type Error = TryFromSliceError;

fn try_from(value: &Bytes) -> Result<Self, TryFromSliceError> {
value.0.as_slice().try_into()
}
}

impl<const N: usize> From<BytesImpl<N>> for Vec<u8> {
fn from(value: BytesImpl<N>) -> Self {
value.to_vec()
Expand Down

0 comments on commit 2d9f5d9

Please sign in to comment.