-
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.
Initial work for Batch codec [skip ci]
- Loading branch information
Showing
9 changed files
with
1,049 additions
and
62 deletions.
There are no files selected for viewing
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,13 +12,14 @@ | |
// ZettaScale Zenoh Team, <[email protected]> | ||
// | ||
use crate::{ | ||
buffer::{Buffer, SplitBuffer}, | ||
reader::HasReader, | ||
vec, | ||
writer::{BacktrackableWriter, DidntWrite, HasWriter, Writer}, | ||
ZSlice, | ||
}; | ||
use alloc::{boxed::Box, sync::Arc}; | ||
use core::{fmt, num::NonZeroUsize}; | ||
use core::{fmt, num::NonZeroUsize, option}; | ||
|
||
#[derive(Clone, PartialEq, Eq)] | ||
pub struct BBuf { | ||
|
@@ -40,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. | ||
|
@@ -77,6 +68,34 @@ impl fmt::Debug for BBuf { | |
} | ||
} | ||
|
||
// 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; | ||
|
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
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 |
---|---|---|
|
@@ -11,14 +11,18 @@ | |
// Contributors: | ||
// ZettaScale Zenoh Team, <[email protected]> | ||
// | ||
use crate::reader::{BacktrackableReader, DidntRead, HasReader, Reader}; | ||
use crate::{ | ||
buffer::{Buffer, SplitBuffer}, | ||
reader::{BacktrackableReader, DidntRead, HasReader, Reader}, | ||
}; | ||
use alloc::{boxed::Box, sync::Arc, vec::Vec}; | ||
use core::{ | ||
any::Any, | ||
convert::AsRef, | ||
fmt, | ||
num::NonZeroUsize, | ||
ops::{Deref, Index, Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive}, | ||
option, | ||
}; | ||
|
||
/*************************************/ | ||
|
@@ -272,6 +276,34 @@ where | |
} | ||
} | ||
|
||
// Buffer | ||
impl Buffer for ZSlice { | ||
fn len(&self) -> usize { | ||
ZSlice::len(self) | ||
} | ||
} | ||
|
||
impl Buffer for &ZSlice { | ||
fn len(&self) -> usize { | ||
ZSlice::len(self) | ||
} | ||
} | ||
|
||
impl Buffer for &mut ZSlice { | ||
fn len(&self) -> usize { | ||
ZSlice::len(self) | ||
} | ||
} | ||
|
||
// SplitBuffer | ||
impl SplitBuffer for ZSlice { | ||
type Slices<'a> = option::IntoIter<&'a [u8]>; | ||
|
||
fn slices(&self) -> Self::Slices<'_> { | ||
Some(self.as_slice()).into_iter() | ||
} | ||
} | ||
|
||
// Reader | ||
impl HasReader for &mut ZSlice { | ||
type Reader = 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.