pub trait View {
// Required methods
fn shape(&self) -> &dyn Shape;
fn get(&self, key: Key) -> Value<'_>;
// Provided method
fn call(
&self,
key: Key,
args: &[Value<'_>],
with: &mut dyn FnMut(Value<'_>),
) { ... }
}Expand description
What a template reads.
use web_net::{Key, Shape, ShapeId, Value, View};
struct Post { title: String, views: i64 }
impl Shape for Post {
fn id(&self) -> ShapeId { ShapeId(1) }
fn key(&self, name: &str) -> Option<Key> {
match name { "title" => Some(Key(0)), "views" => Some(Key(1)), _ => None }
}
}
impl View for Post {
fn shape(&self) -> &dyn Shape { self }
fn get(&self, key: Key) -> Value<'_> {
match key.0 { 0 => Value::Text(&self.title), 1 => Value::Int(self.views), _ => Value::Absent }
}
}
let post = Post { title: "Hello".into(), views: 3 };
let key = post.shape().key("title").unwrap();
assert_eq!(post.get(key).as_text(), Some("Hello"));Required Methods§
Provided Methods§
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".