pub trait Builder<'v>: Sized {
type View: View<Args<'v> = Self>;
}Expand description
Builders are required to build and update views
This is a simple trait that ‘binds’ two types together – a cheap builder and a view.
When the UI creates or updates a view, this builder gets passed to it.
When the user wants to show a view, they can pass its builder to
ui.show() or ui.show_children()
§Note on the 'v lifetime.
This lifetime is provided but you don’t have to use it. If you borrow any data from the application, it must conform to this ’v lifetime. Views cannot borrow data as they must be ’static
§Builders can also be views
// views must be `Debug`, builders don't have to be
#[derive(Debug)]
struct Foo;
impl<'v> Builder<'v> for Foo {
type View = Self; // we cannot borrow anything for 'v because a View must be 'static
}
impl View for Foo {
type Args<'v> = Self; // we can build ourselves
type Response = ();
fn create(args: Self::Args<'_>) -> Self {
// args here is 'Self'
// so we can just return it
// but we can also do Foo{} or Self{}
args
}
// this method has a default implementation which is the same as below
// but we'll implement it for eludication
fn update(&mut self, args: Self::Args<'_>, ui: &Ui) -> Self::Response {
// we're passed 'Foo' (the View of the Builder, which happens to be .. us)
// so we can just replace ourselves with the new one.
//
// if your builder has any parameters provided by the user.
// you should update yourself here. atleast partially.
//
// by default, `update` just replaces calls *self = Self::create()
*self = args;
}
}By convention, you should provide a function that creates your builder with some default parameters. then expose the constructed builder to the user.
The associated view does not have to be public, but the builder should be public
Required Associated Types§
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.