Skip to main content

Capability

Trait Capability 

Source
pub trait Capability:
    Any
    + Send
    + Sync
    + 'static {
    type Value: Clone + Eq + Send + Sync + 'static;

    // Required methods
    fn name(&self) -> &'static str;
    fn supported_values(&self) -> Vec<Self::Value>;
    fn merge(&self, peer_supports: &[Self::Value]) -> Option<Self::Value>;
    fn encode_value(&self, value: &Self::Value) -> Vec<u8> ;
    fn decode_value(&self, bytes: &[u8]) -> Option<Self::Value>;
}
Expand description

Trait every capability implements.

Value is the typed representation of a single supported value. The registry serialises values via Capability::encode_value / Capability::decode_value so no third-party serialisation dependency is required.

§Examples

use dynomite::cluster::capability::Capability;

struct Bool;
impl Capability for Bool {
    type Value = bool;
    fn name(&self) -> &'static str { "feature" }
    fn supported_values(&self) -> Vec<bool> { vec![false, true] }
    fn merge(&self, peer: &[bool]) -> Option<bool> {
        if peer.contains(&true) { Some(true) }
        else if peer.contains(&false) { Some(false) }
        else { None }
    }
    fn encode_value(&self, v: &bool) -> Vec<u8> { vec![u8::from(*v)] }
    fn decode_value(&self, b: &[u8]) -> Option<bool> {
        match b { [0] => Some(false), [1] => Some(true), _ => None }
    }
}

Required Associated Types§

Source

type Value: Clone + Eq + Send + Sync + 'static

The typed value this capability negotiates.

Required Methods§

Source

fn name(&self) -> &'static str

Stable on-the-wire name. Must be ASCII.

Source

fn supported_values(&self) -> Vec<Self::Value>

Locally supported values, ordered from lowest preference to highest preference. The first element is also used as the “floor” when negotiation finds no overlap.

Source

fn merge(&self, peer_supports: &[Self::Value]) -> Option<Self::Value>

Returns the highest local value also supported by peer, or None when there is no overlap. The notion of “highest” is owned by the implementation.

Source

fn encode_value(&self, value: &Self::Value) -> Vec<u8>

Serialise a value to a stable byte sequence. Used to build the on-the-wire advertisement.

Source

fn decode_value(&self, bytes: &[u8]) -> Option<Self::Value>

Inverse of Capability::encode_value. Returning None causes the registry to drop the malformed value when merging a peer ad.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§