Skip to content

Commit

Permalink
Allowed more conversions with ByteBuf for use in BPX.
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuri6037 committed Nov 23, 2023
1 parent 9c2bc08 commit 2623f57
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bytesutil"
version = "0.4.0"
version = "0.5.0"
authors = ["Yuri Edward <yuri6037@outlook.com>"]
edition = "2021"
description = "Yet another byte utility for Rust"
Expand Down
35 changes: 35 additions & 0 deletions src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,36 @@ impl<T> ByteBuf<T> {
}
}

impl<T> From<T> for ByteBuf<T> {
fn from(value: T) -> Self {
Self::new(value)
}
}

impl<T: Copy> From<&T> for ByteBuf<T> {
fn from(value: &T) -> Self {
Self::new(*value)
}
}

impl<T: Copy> From<&mut T> for ByteBuf<T> {
fn from(value: &mut T) -> Self {
Self::new(*value)
}
}

impl<T: Copy> From<&ByteBuf<T>> for ByteBuf<T> {
fn from(value: &ByteBuf<T>) -> Self {
*value
}
}

impl<T: Copy> From<&mut ByteBuf<T>> for ByteBuf<T> {
fn from(value: &mut ByteBuf<T>) -> Self {
*value
}
}

impl<T: Default> Default for ByteBuf<T> {
fn default() -> Self {
Self { inner: Default::default() }
Expand Down Expand Up @@ -129,12 +159,17 @@ pub type StaticByteBuf<const N: usize> = ByteBuf<[u8; N]>;
mod tests {
use crate::{StaticByteBuf, ByteBuf};

fn test_function<'a, I: Into<ByteBuf<[u8; 16]>>>(_: I) {
}

#[test]
fn basic() {
let mut buffer = StaticByteBuf::<16>::default();
buffer.set_le(0, 42).set_be(8, 42.42);
assert!(buffer.get_le::<i32>(0) == 42);
assert!(buffer.get_be::<f64>(8) == 42.42);
test_function(buffer.set_le(0, 12));
test_function([0; 16]);
}

#[test]
Expand Down

0 comments on commit 2623f57

Please sign in to comment.