Skip to main content

Resource

Trait Resource 

Source
pub trait Resource:
    Send
    + Sync
    + Debug {
    // Required methods
    fn type_url(&self) -> &str;
    fn name(&self) -> &str;
    fn encode(&self) -> Result<Any, Box<dyn Error + Send + Sync>>;
    fn as_any(&self) -> &dyn Any;

    // Provided method
    fn version(&self) -> Option<&str> { ... }
}
Expand description

Trait for xDS resources.

Implement this trait to create custom xDS resource types that can be stored in the cache and served via xDS.

§Example

use xds_core::{Resource, TypeUrl};
use prost_types::Any;
use std::any::Any as StdAny;

#[derive(Debug)]
struct MyCluster {
    name: String,
    // ... other fields
}

impl Resource for MyCluster {
    fn type_url(&self) -> &str {
        TypeUrl::CLUSTER
    }

    fn name(&self) -> &str {
        &self.name
    }

    fn encode(&self) -> Result<Any, Box<dyn std::error::Error + Send + Sync>> {
        // Encode to protobuf Any
        Ok(Any {
            type_url: self.type_url().to_string(),
            value: vec![], // actual encoding would go here
        })
    }

    fn as_any(&self) -> &dyn StdAny {
        self
    }
}

Required Methods§

Source

fn type_url(&self) -> &str

Get the type URL for this resource.

Source

fn name(&self) -> &str

Get the resource name.

Source

fn encode(&self) -> Result<Any, Box<dyn Error + Send + Sync>>

Encode the resource to a protobuf Any message.

Source

fn as_any(&self) -> &dyn Any

Convert to Any for downcasting.

Provided Methods§

Source

fn version(&self) -> Option<&str>

Get the resource version, if known.

Implementors§