Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small update to Bytes/BytesImpl/Program conversions #527

Merged
merged 1 commit into from
May 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading