-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
86 changed files
with
3,641 additions
and
1,783 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,14 +12,16 @@ | |
// ZettaScale Zenoh Team, <[email protected]> | ||
// | ||
use crate::{ | ||
buffer::{Buffer, SplitBuffer}, | ||
reader::HasReader, | ||
vec, | ||
writer::{BacktrackableWriter, DidntWrite, HasWriter, Writer}, | ||
ZSlice, | ||
}; | ||
use alloc::boxed::Box; | ||
use core::num::NonZeroUsize; | ||
use alloc::{boxed::Box, sync::Arc}; | ||
use core::{fmt, num::NonZeroUsize, option}; | ||
|
||
#[derive(Clone, Debug, PartialEq, Eq)] | ||
#[derive(Clone, PartialEq, Eq)] | ||
pub struct BBuf { | ||
buffer: Box<[u8]>, | ||
len: usize, | ||
|
@@ -39,16 +41,6 @@ impl BBuf { | |
self.buffer.len() | ||
} | ||
|
||
#[must_use] | ||
pub const fn len(&self) -> usize { | ||
self.len | ||
} | ||
|
||
#[must_use] | ||
pub const fn is_empty(&self) -> bool { | ||
self.len == 0 | ||
} | ||
|
||
#[must_use] | ||
pub fn as_slice(&self) -> &[u8] { | ||
// SAFETY: self.len is ensured by the writer to be smaller than buffer length. | ||
|
@@ -70,6 +62,40 @@ impl BBuf { | |
} | ||
} | ||
|
||
impl fmt::Debug for BBuf { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
write!(f, "{:02x?}", self.as_slice()) | ||
} | ||
} | ||
|
||
// Buffer | ||
impl Buffer for BBuf { | ||
fn len(&self) -> usize { | ||
self.len | ||
} | ||
} | ||
|
||
impl Buffer for &BBuf { | ||
fn len(&self) -> usize { | ||
self.len | ||
} | ||
} | ||
|
||
impl Buffer for &mut BBuf { | ||
fn len(&self) -> usize { | ||
self.len | ||
} | ||
} | ||
|
||
// SplitBuffer | ||
impl SplitBuffer for BBuf { | ||
type Slices<'a> = option::IntoIter<&'a [u8]>; | ||
|
||
fn slices(&self) -> Self::Slices<'_> { | ||
Some(self.as_slice()).into_iter() | ||
} | ||
} | ||
|
||
// Writer | ||
impl HasWriter for &mut BBuf { | ||
type Writer = Self; | ||
|
@@ -152,6 +178,19 @@ impl<'a> HasReader for &'a BBuf { | |
} | ||
} | ||
|
||
// From impls | ||
impl From<BBuf> for ZSlice { | ||
fn from(value: BBuf) -> Self { | ||
ZSlice { | ||
buf: Arc::new(value.buffer), | ||
start: 0, | ||
end: value.len, | ||
#[cfg(feature = "shared-memory")] | ||
kind: crate::ZSliceKind::Raw, | ||
} | ||
} | ||
} | ||
|
||
#[cfg(feature = "test")] | ||
impl BBuf { | ||
pub fn rand(len: usize) -> Self { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,11 +12,42 @@ | |
// ZettaScale Zenoh Team, <[email protected]> | ||
// | ||
use crate::{ | ||
buffer::{Buffer, SplitBuffer}, | ||
reader::{BacktrackableReader, DidntRead, DidntSiphon, HasReader, Reader, SiphonableReader}, | ||
writer::{BacktrackableWriter, DidntWrite, HasWriter, Writer}, | ||
ZSlice, | ||
}; | ||
use core::{marker::PhantomData, mem, num::NonZeroUsize, slice}; | ||
use core::{ | ||
marker::PhantomData, | ||
mem, | ||
num::NonZeroUsize, | ||
option, | ||
slice::{self}, | ||
}; | ||
|
||
// Buffer | ||
impl Buffer for &[u8] { | ||
#[inline(always)] | ||
fn len(&self) -> usize { | ||
<[u8]>::len(self) | ||
} | ||
} | ||
|
||
impl Buffer for &mut [u8] { | ||
#[inline(always)] | ||
fn len(&self) -> usize { | ||
<[u8]>::len(self) | ||
} | ||
} | ||
|
||
// SplitBuffer | ||
impl<'b> SplitBuffer for &'b [u8] { | ||
type Slices<'a> = option::IntoIter<&'a [u8]> where 'b: 'a; | ||
|
||
fn slices(&self) -> Self::Slices<'_> { | ||
Some(*self).into_iter() | ||
} | ||
} | ||
|
||
// Writer | ||
impl HasWriter for &mut [u8] { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,11 +12,12 @@ | |
// ZettaScale Zenoh Team, <[email protected]> | ||
// | ||
use crate::{ | ||
buffer::{Buffer, SplitBuffer}, | ||
reader::HasReader, | ||
writer::{BacktrackableWriter, DidntWrite, HasWriter, Writer}, | ||
}; | ||
use alloc::vec::Vec; | ||
use core::{mem, num::NonZeroUsize}; | ||
use core::{mem, num::NonZeroUsize, option}; | ||
|
||
/// Allocate a vector with a given capacity and sets the length to that capacity. | ||
#[must_use] | ||
|
@@ -30,6 +31,34 @@ pub fn uninit(capacity: usize) -> Vec<u8> { | |
vbuf | ||
} | ||
|
||
// Buffer | ||
impl Buffer for Vec<u8> { | ||
fn len(&self) -> usize { | ||
Vec::len(self) | ||
} | ||
} | ||
|
||
impl Buffer for &Vec<u8> { | ||
fn len(&self) -> usize { | ||
Vec::len(self) | ||
} | ||
} | ||
|
||
impl Buffer for &mut Vec<u8> { | ||
fn len(&self) -> usize { | ||
Vec::len(self) | ||
} | ||
} | ||
|
||
// SplitBuffer | ||
impl SplitBuffer for Vec<u8> { | ||
type Slices<'a> = option::IntoIter<&'a [u8]>; | ||
|
||
fn slices(&self) -> Self::Slices<'_> { | ||
Some(self.as_slice()).into_iter() | ||
} | ||
} | ||
|
||
// Writer | ||
impl<'a> HasWriter for &'a mut Vec<u8> { | ||
type Writer = Self; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.