pub struct ZBytes(/* private fields */);Expand description
ZBytes contains the raw bytes data.
This type is intended to represent the data payload with minimized copying.
Zenoh may construct a single ZBytes instance from pointers to multiple buffers
in cases where data is received fragmented from the network.
To directly access raw data as a contiguous slice, it is preferable to convert ZBytes into a [std::borrow::Cow<[u8]>] using to_bytes.
If ZBytes contains all the data in a single memory location, this is guaranteed to be zero-copy. This is the common case for small messages.
If ZBytes contains data scattered in different memory regions, this operation will do an allocation and a copy. This is the common case for large messages.
It is also possible to iterate over the raw data that may be scattered across different memory regions using slices.
Another way to access raw data is to use a ZBytesReader obtained from reader
that implements the standard std::io::Read trait. This is useful when deserializing data using
libraries that operate on std::io::Read.
The creation of a ZBytes instance using the std::io::Write trait is also possible using the static
writer method that creates a ZBytesWriter.
§Examples
ZBytes can be converted from/to raw bytes:
use std::borrow::Cow;
use zenoh::bytes::ZBytes;
let buf = b"some raw bytes";
let payload = ZBytes::from(buf);
assert_eq!(payload.to_bytes(), buf.as_slice());Create a ZBytes with a writer and read it back with a reader:
use std::io::{Read, Write};
use zenoh::bytes::ZBytes;
let mut writer = ZBytes::writer();
writer.write_all(b"some raw bytes").unwrap();
let payload = writer.finish();
let mut reader = payload.reader();
let mut buf = [0; 14];
reader.read_exact(&mut buf).unwrap();
assert_eq!(&buf, b"some raw bytes");Implementations§
Source§impl ZBytes
impl ZBytes
Sourcepub fn to_bytes(&self) -> Cow<'_, [u8]>
pub fn to_bytes(&self) -> Cow<'_, [u8]>
Access raw bytes contained in the ZBytes.
In the case ZBytes contains non-contiguous regions of memory, an allocation and a copy
will be done; that’s why the method returns a Cow.
It’s also possible to use ZBytes::slices instead to avoid this copy.
Sourcepub fn try_to_string(&self) -> Result<Cow<'_, str>, Utf8Error>
pub fn try_to_string(&self) -> Result<Cow<'_, str>, Utf8Error>
Tries to access a string contained in the ZBytes, and fails if it contains non-UTF-8 bytes.
In the case ZBytes contains non-contiguous regions of memory, an allocation and a copy
will be done; that’s why the method returns a Cow.
It’s also possible to use ZBytes::slices instead to avoid this copy, but then the UTF-8
check has to be done manually.
Sourcepub fn reader(&self) -> ZBytesReader<'_> ⓘ
pub fn reader(&self) -> ZBytesReader<'_> ⓘ
Get a ZBytesReader implementing the std::io::Read trait.
See ZBytesWriter on how to chain the deserialization of different types from a single ZBytes.
Sourcepub fn from_reader<R>(reader: R) -> Result<Self, Error>where
R: Read,
pub fn from_reader<R>(reader: R) -> Result<Self, Error>where
R: Read,
Build a ZBytes from a generic reader implementing std::io::Read. This operation copies data from the reader.
Sourcepub fn writer() -> ZBytesWriter ⓘ
pub fn writer() -> ZBytesWriter ⓘ
Get a ZBytesWriter implementing std::io::Write trait.
See ZBytesWriter on how to chain the serialization of different types into a single ZBytes.
Sourcepub fn slices(&self) -> ZBytesSliceIterator<'_> ⓘ
pub fn slices(&self) -> ZBytesSliceIterator<'_> ⓘ
Return an iterator over raw byte slices contained in the ZBytes.
ZBytes may store data in non-contiguous regions of memory; this iterator
then allows accessing raw data directly without any attempt at deserializing it.
Please note that no guarantee is provided on the internal memory layout of ZBytes.
The only provided guarantee is that the byte order is preserved.
use std::io::Write;
use zenoh::bytes::ZBytes;
let buf1: Vec<u8> = vec![1, 2, 3];
let buf2: Vec<u8> = vec![4, 5, 6, 7, 8];
let mut writer = ZBytes::writer();
writer.write(&buf1);
writer.write(&buf2);
let zbytes = writer.finish();
// Access the raw content
for slice in zbytes.slices() {
println!("{:02x?}", slice);
}
// Concatenate input in a single vector
let buf: Vec<u8> = buf1.into_iter().chain(buf2.into_iter()).collect();
// Concatenate raw bytes in a single vector
let out: Vec<u8> = zbytes.slices().fold(Vec::new(), |mut b, x| { b.extend_from_slice(x); b });
// The previous line is the equivalent of
// let out: Vec<u8> = zbs.into();
assert_eq!(buf, out);The example below shows how the ZBytesWriter::append simply appends the slices of one ZBytes
to another and how those slices can be iterated over to access the raw data.
use std::io::Write;
use zenoh::bytes::ZBytes;
let buf1: Vec<u8> = vec![1, 2, 3];
let buf2: Vec<u8> = vec![4, 5, 6, 7, 8];
let mut writer = ZBytes::writer();
writer.append(ZBytes::from(buf1.clone()));
writer.append(ZBytes::from(buf2.clone()));
let zbytes = writer.finish();
let mut iter = zbytes.slices();
assert_eq!(buf1.as_slice(), iter.next().unwrap());
assert_eq!(buf2.as_slice(), iter.next().unwrap());Trait Implementations§
Source§impl<const ID: u8> From<AttachmentType<ID>> for ZBytes
impl<const ID: u8> From<AttachmentType<ID>> for ZBytes
Source§fn from(this: AttachmentType<ID>) -> Self
fn from(this: AttachmentType<ID>) -> Self
Source§impl<T, Buf: Into<ZBytes>> From<Typed<T, Buf>> for ZBytes
Available on crate features unstable and shared-memory only.
impl<T, Buf: Into<ZBytes>> From<Typed<T, Buf>> for ZBytes
unstable and shared-memory only.Source§impl From<ZBytesWriter> for ZBytes
impl From<ZBytesWriter> for ZBytes
Source§fn from(value: ZBytesWriter) -> Self
fn from(value: ZBytesWriter) -> Self
impl Eq for ZBytes
impl StructuralPartialEq for ZBytes
Auto Trait Implementations§
impl Freeze for ZBytes
impl !RefUnwindSafe for ZBytes
impl Send for ZBytes
impl Sync for ZBytes
impl Unpin for ZBytes
impl UnsafeUnpin for ZBytes
impl !UnwindSafe for ZBytes
Blanket Implementations§
Source§impl<Source> AccessAs for Source
impl<Source> AccessAs for Source
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more