Skip to content

Commit

Permalink
feat: use bytemuck for VarInt slice conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
wyfo committed Sep 27, 2024
1 parent ddf3ee5 commit 1b80456
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 34 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ async-trait = "0.1.82"
base64 = "0.22.1"
bincode = "1.3.3"
bytes = "1.7.1"
bytemuck = "1"
clap = { version = "4.5.17", features = ["derive"] }
console-subscriber = "0.4.0"
const_format = "0.2.33"
Expand Down
1 change: 1 addition & 0 deletions zenoh-ext/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ tokio = { workspace = true, features = [
"io-std",
] }
bincode = { workspace = true }
bytemuck = { workspace = true }
zenoh-util = { workspace = true }
flume = { workspace = true }
futures = { workspace = true }
Expand Down
59 changes: 25 additions & 34 deletions zenoh-ext/src/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::{
hash::Hash,
io::{Read, Write},
marker::PhantomData,
ops::{Deref, DerefMut},
ptr,
};

Expand Down Expand Up @@ -366,39 +367,39 @@ impl_tuple!(

#[repr(transparent)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct VarInt<T>(T);
pub struct VarInt<T>(pub T);

impl<T> VarInt<T> {
pub fn new(int: T) -> Self {
Self(int)
}
unsafe impl<T> bytemuck::TransparentWrapper<T> for VarInt<T> {}
impl<T> Deref for VarInt<T> {
type Target = T;

pub fn from_ref(int: &T) -> &Self {
// SAFETY: VInt is repr(transparent)
unsafe { &*(int as *const T as *const Self) }
fn deref(&self) -> &Self::Target {
&self.0
}

pub fn into_inner(self) -> T {
self.0
}
impl<T> DerefMut for VarInt<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}

pub fn as_inner(&self) -> &T {
&self.0
}
impl<T> From<T> for VarInt<T> {
fn from(value: T) -> Self {
Self(value)
}
}
impl<T> AsRef<T> for VarInt<T> {
fn as_ref(&self) -> &T {
self
}
}
impl<T> AsMut<T> for VarInt<T> {
fn as_mut(&mut self) -> &mut T {
self
}
}

macro_rules! impl_varint {
($($u:ty: $i:ty),* $(,)?) => {$(
impl From<$u> for VarInt<$u> {
fn from(value: $u) -> Self {
Self(value)
}
}
impl From<$i> for VarInt<$i> {
fn from(value: $i) -> Self {
Self(value)
}
}
impl From<VarInt<$u>> for $u {
fn from(value: VarInt<$u>) -> Self {
value.0
Expand All @@ -409,16 +410,6 @@ macro_rules! impl_varint {
value.0
}
}
impl AsRef<$u> for VarInt<$u> {
fn as_ref(&self) -> &$u {
&self.0
}
}
impl AsRef<$i> for VarInt<$i> {
fn as_ref(&self) -> &$i {
&self.0
}
}
impl Serialize for VarInt<$u> {
fn serialize(&self, serializer: &mut ZSerializer) {
leb128::write::unsigned(&mut serializer.0, self.0 as u64).unwrap();
Expand Down

0 comments on commit 1b80456

Please sign in to comment.