Encoding

Struct Encoding 

Source
pub struct Encoding(/* private fields */);
Expand description

§Encoding information

The Sample or ReplyError may contain the optional encoding value, which indicates how the payload should be interpreted by the application.

The encoding is represented as a string in MIME-like format: type/subtype[;schema].

To optimize network usage, Zenoh internally maps some predefined encoding strings to an integer identifier. These encodings are provided as associated constants of the Encoding struct, e.g. Encoding::ZENOH_BYTES, Encoding::APPLICATION_JSON, etc. This internal mapping is not exposed to the application layer and, from the API point of view, the encoding is always represented as a string. But it’s useful to know that some encodings are much more efficient than others, especially considering that the encoding value is sent with each message.

Please note that the Zenoh protocol does not impose any encoding value, nor does it operate on it. It can be seen as optional metadata that is carried over by Zenoh in such a way that the application may perform different operations depending on the encoding value.

§Examples

§String operations

Create an Encoding from a string and vice versa.

use zenoh::bytes::Encoding;

let encoding: Encoding = "text/plain".into();
let text: String = encoding.clone().into();
assert_eq!("text/plain", &text);

§Constants and cow operations

Since some encoding values are internally optimized by Zenoh, it’s generally more efficient to use the defined constants and Cow conversion to obtain its string representation.

use zenoh::bytes::Encoding;
use std::borrow::Cow;

// This allocates
assert_eq!("text/plain", &String::from(Encoding::TEXT_PLAIN));
// This does NOT allocate
assert_eq!("text/plain", &Cow::from(Encoding::TEXT_PLAIN));

§Schema

Additionally, a schema can be associated with the encoding. The convention is to use the ; separator if an encoding is created from a string. Alternatively, with_schema() can be used to add a schema to one of the associated constants.

use zenoh::bytes::Encoding;

let encoding1 = Encoding::from("text/plain;utf-8");
let encoding2 = Encoding::TEXT_PLAIN.with_schema("utf-8");
assert_eq!(encoding1, encoding2);
assert_eq!("text/plain;utf-8", &encoding1.to_string());
assert_eq!("text/plain;utf-8", &encoding2.to_string());

Implementations§

Source§

impl Encoding

Source

pub const ZENOH_BYTES: Encoding

Just some bytes.

Constant alias for string: "zenoh/bytes".

This encoding assumes that the payload was created with ZBytes::from::<Vec<u8>> or similar ([u8], [u8;N], Cow<_,[u8]>) and its data can be accessed with ZBytes::to_bytes(), no additional assumptions about data format are made.

Source

pub const ZENOH_STRING: Encoding

A UTF-8 string.

Constant alias for string: "zenoh/string".

This encoding assumes that the payload was created with ZBytes::from::<String> or similar (&str, Cow<str>) and its data can be accessed with ZBytes::try_to_string() without an error.

Source

pub const ZENOH_SERIALIZED: Encoding

Zenoh serialized data.

Constant alias for string: "zenoh/serialized".

This encoding assumes that the payload was created with serialization functions provided by the zenoh-ext crate. The schema field may contain the details of the serialization format.

Source

pub const APPLICATION_OCTET_STREAM: Encoding

An application-specific stream of bytes.

Constant alias for string: "application/octet-stream".

Source

pub const TEXT_PLAIN: Encoding

A textual file.

Constant alias for string: "text/plain".

Source

pub const APPLICATION_JSON: Encoding

JSON data intended to be consumed by an application.

Constant alias for string: "application/json".

Source

pub const TEXT_JSON: Encoding

JSON data intended to be human readable.

Constant alias for string: "text/json".

Source

pub const APPLICATION_CDR: Encoding

Common Data Representation (CDR)-encoded data.

Constant alias for string: "application/cdr".

Source

pub const APPLICATION_CBOR: Encoding

Concise Binary Object Representation (CBOR)-encoded data.

Constant alias for string: "application/cbor".

Source

pub const APPLICATION_YAML: Encoding

YAML data intended to be consumed by an application.

Constant alias for string: "application/yaml".

Source

pub const TEXT_YAML: Encoding

YAML data intended to be human readable.

Constant alias for string: "text/yaml".

Source

pub const TEXT_JSON5: Encoding

JSON5-encoded data that are human readable.

Constant alias for string: "text/json5".

Source

pub const APPLICATION_PYTHON_SERIALIZED_OBJECT: Encoding

A Python object serialized using pickle.

Constant alias for string: "application/python-serialized-object".

Source

pub const APPLICATION_PROTOBUF: Encoding

Application-specific protobuf-encoded data.

Constant alias for string: "application/protobuf".

Source

pub const APPLICATION_JAVA_SERIALIZED_OBJECT: Encoding

A Java serialized object.

Constant alias for string: "application/java-serialized-object".

Source

pub const APPLICATION_OPENMETRICS_TEXT: Encoding

OpenMetrics data, commonly used by Prometheus.

Constant alias for string: "application/openmetrics-text".

Source

pub const IMAGE_PNG: Encoding

A Portable Network Graphics (PNG) image.

Constant alias for string: "image/png".

Source

pub const IMAGE_JPEG: Encoding

A Joint Photographic Experts Group (JPEG) image.

Constant alias for string: "image/jpeg".

Source

pub const IMAGE_GIF: Encoding

A Graphics Interchange Format (GIF) image.

Constant alias for string: "image/gif".

Source

pub const IMAGE_BMP: Encoding

A Bitmap (BMP) image.

Constant alias for string: "image/bmp".

Source

pub const IMAGE_WEBP: Encoding

A WebP image.

Constant alias for string: "image/webp".

Source

pub const APPLICATION_XML: Encoding

An XML file intended to be consumed by an application.

Constant alias for string: "application/xml".

Source

pub const APPLICATION_X_WWW_FORM_URLENCODED: Encoding

An encoded list of tuples, each consisting of a name and a value.

Constant alias for string: "application/x-www-form-urlencoded".

Source

pub const TEXT_HTML: Encoding

An HTML file.

Constant alias for string: "text/html".

Source

pub const TEXT_XML: Encoding

An XML file that is human readable.

Constant alias for string: "text/xml".

Source

pub const TEXT_CSS: Encoding

A CSS file.

Constant alias for string: "text/css".

Source

pub const TEXT_JAVASCRIPT: Encoding

A JavaScript file.

Constant alias for string: "text/javascript".

Source

pub const TEXT_MARKDOWN: Encoding

A Markdown file.

Constant alias for string: "text/markdown".

Source

pub const TEXT_CSV: Encoding

A CSV file.

Constant alias for string: "text/csv".

Source

pub const APPLICATION_SQL: Encoding

An application-specific SQL query.

Constant alias for string: "application/sql".

Source

pub const APPLICATION_COAP_PAYLOAD: Encoding

Constrained Application Protocol (CoAP) data intended for CoAP-to-HTTP and HTTP-to-CoAP proxies.

Constant alias for string: "application/coap-payload".

Source

pub const APPLICATION_JSON_PATCH_JSON: Encoding

Defines a JSON document structure for expressing a sequence of operations to apply to a JSON document.

Constant alias for string: "application/json-patch+json".

Source

pub const APPLICATION_JSON_SEQ: Encoding

A JSON text sequence consists of any number of JSON texts, all encoded in UTF-8.

Constant alias for string: "application/json-seq".

Source

pub const APPLICATION_JSONPATH: Encoding

JSONPath defines a string syntax for selecting and extracting JSON values from within a given JSON value.

Constant alias for string: "application/jsonpath".

Source

pub const APPLICATION_JWT: Encoding

A JSON Web Token (JWT).

Constant alias for string: "application/jwt".

Source

pub const APPLICATION_MP4: Encoding

Application-specific MPEG-4-encoded data, either audio or video.

Constant alias for string: "application/mp4".

Source

pub const APPLICATION_SOAP_XML: Encoding

A SOAP 1.2 message serialized as XML 1.0.

Constant alias for string: "application/soap+xml".

Source

pub const APPLICATION_YANG: Encoding

YANG-encoded data commonly used by the Network Configuration Protocol (NETCONF).

Constant alias for string: "application/yang".

Source

pub const AUDIO_AAC: Encoding

An MPEG-4 Advanced Audio Coding (AAC) media.

Constant alias for string: "audio/aac".

Source

pub const AUDIO_FLAC: Encoding

A Free Lossless Audio Codec (FLAC) media.

Constant alias for string: "audio/flac".

Source

pub const AUDIO_MP4: Encoding

An audio codec defined in MPEG-1, MPEG-2, MPEG-4, or registered at the MP4 registration authority.

Constant alias for string: "audio/mp4".

Source

pub const AUDIO_OGG: Encoding

An Ogg-encapsulated audio stream.

Constant alias for string: "audio/ogg".

Source

pub const AUDIO_VORBIS: Encoding

A Vorbis-encoded audio stream.

Constant alias for string: "audio/vorbis".

Source

pub const VIDEO_H261: Encoding

An h261-encoded video stream.

Constant alias for string: "video/h261".

Source

pub const VIDEO_H263: Encoding

An h263-encoded video stream.

Constant alias for string: "video/h263".

Source

pub const VIDEO_H264: Encoding

An h264-encoded video stream.

Constant alias for string: "video/h264".

Source

pub const VIDEO_H265: Encoding

An h265-encoded video stream.

Constant alias for string: "video/h265".

Source

pub const VIDEO_H266: Encoding

An h266-encoded video stream.

Constant alias for string: "video/h266".

Source

pub const VIDEO_MP4: Encoding

A video codec defined in MPEG-1, MPEG-2, MPEG-4, or registered at the MP4 registration authority.

Constant alias for string: "video/mp4".

Source

pub const VIDEO_OGG: Encoding

An Ogg-encapsulated video stream.

Constant alias for string: "video/ogg".

Source

pub const VIDEO_RAW: Encoding

An uncompressed, studio-quality video stream.

Constant alias for string: "video/raw".

Source

pub const VIDEO_VP8: Encoding

A VP8-encoded video stream.

Constant alias for string: "video/vp8".

Source

pub const VIDEO_VP9: Encoding

A VP9-encoded video stream.

Constant alias for string: "video/vp9".

Source

pub const fn default() -> Self

The default Encoding is ZENOH_BYTES.

Source

pub fn with_schema<S>(self, s: S) -> Self
where S: Into<String>,

Set a schema for this encoding. Zenoh does not define what a schema is, and its semantics is left to the implementer. E.g., a common schema for the text/plain encoding is utf-8.

Trait Implementations§

Source§

impl Clone for Encoding

Source§

fn clone(&self) -> Encoding

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Encoding

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Encoding

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Display for Encoding

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<&Encoding> for Cow<'static, str>

Source§

fn from(encoding: &Encoding) -> Self

Converts to this type from the input type.
Source§

impl From<&str> for Encoding

Source§

fn from(t: &str) -> Self

Converts to this type from the input type.
Source§

impl From<Encoding> for Cow<'static, str>

Source§

fn from(encoding: Encoding) -> Self

Converts to this type from the input type.
Source§

impl From<Encoding> for Encoding

Source§

fn from(value: Encoding) -> Self

Converts to this type from the input type.
Source§

impl From<Encoding> for Encoding

Source§

fn from(value: Encoding) -> Self

Converts to this type from the input type.
Source§

impl From<Encoding> for String

Source§

fn from(encoding: Encoding) -> Self

Converts to this type from the input type.
Source§

impl From<String> for Encoding

Source§

fn from(value: String) -> Self

Converts to this type from the input type.
Source§

impl FromStr for Encoding

Source§

type Err = Infallible

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl Hash for Encoding

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Encoding

Source§

fn eq(&self, other: &Encoding) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for Encoding

Source§

impl StructuralPartialEq for Encoding

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<Source> AccessAs for Source

Source§

fn ref_as<T>(&self) -> <Source as IGuardRef<T>>::Guard<'_>
where Source: IGuardRef<T>, T: ?Sized,

Provides immutable access to a type as if it were its ABI-unstable equivalent.
Source§

fn mut_as<T>(&mut self) -> <Source as IGuardMut<T>>::GuardMut<'_>
where Source: IGuardMut<T>, T: ?Sized,

Provides mutable access to a type as if it were its ABI-unstable equivalent.
Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> AsNode<T> for T

Source§

fn as_node(&self) -> &T

Source§

impl<T> AsNodeMut<T> for T

Source§

fn as_node_mut(&mut self) -> &mut T

Source§

impl<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

Source§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

Source§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

Source§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, As> IGuardMut<As> for T
where T: Into<As>, As: Into<T>,

Source§

type GuardMut<'a> = MutAs<'a, T, As> where T: 'a

The type of the guard which will clean up the temporary after applying its changes to the original.
Source§

fn guard_mut_inner(&mut self) -> <T as IGuardMut<As>>::GuardMut<'_>

Construct the temporary and guard it through a mutable reference.
Source§

impl<T, As> IGuardRef<As> for T
where T: Into<As>, As: Into<T>,

Source§

type Guard<'a> = RefAs<'a, T, As> where T: 'a

The type of the guard which will clean up the temporary.
Source§

fn guard_ref_inner(&self) -> <T as IGuardRef<As>>::Guard<'_>

Construct the temporary and guard it through an immutable reference.
Source§

impl<T> Includes<End> for T

Source§

type Output = End

The result
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more