Skip to main content

Envelope

Struct Envelope 

pub struct Envelope<T> { /* private fields */ }
Expand description

A replication message, wrapped with the wire format version that produced it.

Field order is part of the contract: format_version is declared first so that a positional encoder writes it first. See the module documentation for why that is only half of WF-8’s obligation.

use happenstance_sync::wire::{Envelope, FORMAT_VERSION};

let envelope = Envelope::new("hello");
assert_eq!(envelope.format_version(), FORMAT_VERSION);

let json = serde_json::to_string(&envelope)?;
assert_eq!(json, r#"{"format_version":3,"message":"hello"}"#);

let decoded: Envelope<String> = serde_json::from_str(&json)?;
assert_eq!(decoded.message(), "hello");

A version this build does not implement is refused, and the message inside is never decoded:

use happenstance_sync::wire::{Envelope, WireError};

// What a peer one format version ahead would send.
let json = r#"{"format_version":999,"message":"hello"}"#;
let error = serde_json::from_str::<Envelope<String>>(json).unwrap_err();
assert!(error.to_string().contains(WireError::UNSUPPORTED_FORMAT_VERSION));

Implementations§

§

impl<T> Envelope<T>

pub const fn new(message: T) -> Self

Wraps message and stamps FORMAT_VERSION.

There is deliberately no constructor that stamps an arbitrary version: a sender that can name a version it does not implement can send one, and nothing downstream could tell that from a peer that genuinely speaks it.

pub const fn format_version(&self) -> u16

The version this envelope was encoded at.

Always FORMAT_VERSION — a decoded envelope carrying anything else does not exist, because decoding refuses it.

pub const fn message(&self) -> &T

The message the envelope carries.

pub fn into_message(self) -> T

Unwraps the envelope, discarding the version.

Trait Implementations§

§

impl<T: Clone> Clone for Envelope<T>

§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
§

impl<T: Copy> Copy for Envelope<T>

§

impl<T: Debug> Debug for Envelope<T>

§

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

Formats the value using the given formatter. Read more
§

impl<'de, T: Deserialize<'de>> Deserialize<'de> for Envelope<T>

The hand-written half of the format, and the reason this module exists.

Do not replace this with #[derive(Deserialize)]. The derive is a code generator with no interception point: it reads every field into a local Option and only then constructs the struct, so a derived Envelope<T> decodes the message before anything examines the version — the partial decode WF-8’s MUST NOT forbids. default, deny_unknown_fields and flatten are the hooks the derive offers, and none of them is “stop here and decide”.

A derive plus a version check afterwards looks identical from outside and is not: measured, it returns the same Err in both formats and produces byte-identical postcard framing — [01 07] at version 1, [80 03 07] at 384, the same take_from_bytes::<u16> value and the same [07] remainder. The one observation that separates them is whether T::deserialize ran at all, which is what wire::version_is_readable_before_the_message witnesses.

§

fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>

Deserialize this value from the given Serde deserializer. Read more
§

impl<T: Eq> Eq for Envelope<T>

§

impl<T: Hash> Hash for Envelope<T>

§

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
§

impl<T: PartialEq> PartialEq for Envelope<T>

§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
§

impl<T> Serialize for Envelope<T>
where T: Serialize,

§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
§

impl<T: PartialEq> StructuralPartialEq for Envelope<T>

Auto Trait Implementations§

§

impl<T> Freeze for Envelope<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Envelope<T>
where T: RefUnwindSafe,

§

impl<T> Send for Envelope<T>
where T: Send,

§

impl<T> Sync for Envelope<T>
where T: Sync,

§

impl<T> Unpin for Envelope<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for Envelope<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for Envelope<T>
where T: UnwindSafe,

Blanket Implementations§

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> 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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.