Skip to main content

View

Trait View 

Source
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§

Source

fn shape(&self) -> &dyn Shape

The names this view answers.

Source

fn get(&self, key: Key) -> Value<'_>

A field by key. The hot path: no allocation.

Provided Methods§

Source

fn call(&self, key: Key, args: &[Value<'_>], with: &mut dyn FnMut(Value<'_>))

A method with arguments (variations.options("size")). The result is passed to with, so it may borrow a value computed for the call. The default answers Value::Absent.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§

Source§

impl View for Fields<'_>