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
}
}