BxAdapter

Trait BxAdapter 

Source
pub trait BxAdapter {
    // Provided methods
    fn start<B>(builder: B) -> B { ... }
    fn attach<B, C>(builder: B, child: C) -> B
       where Self: CanAttach<B, C> { ... }
    fn attach_many<B, I, C>(builder: B, children: I) -> B
       where Self: CanAttachMany<B, I, C> { ... }
}
Expand description

Adapter hook to make the builder DSL agnostic to concrete APIs.

§Examples

Implement a custom adapter that forwards to methods on your builder type:

use builderx_core::{BxAdapter, CanAttach, CanAttachMany};

#[derive(Default, Debug, PartialEq)]
struct MyBuilder(Vec<&'static str>);

impl MyBuilder {
    fn push_one(mut self, child: &'static str) -> Self {
        self.0.push(child);
        self
    }

    fn push_many<I>(mut self, children: I) -> Self
    where
        I: IntoIterator<Item = &'static str>,
    {
        self.0.extend(children);
        self
    }
}

struct MyAdapter;

impl BxAdapter for MyAdapter {}

impl CanAttach<MyBuilder, &'static str> for MyAdapter {
    fn do_attach(builder: MyBuilder, child: &'static str) -> MyBuilder {
        builder.push_one(child)
    }
}

impl<I> CanAttachMany<MyBuilder, I, &'static str> for MyAdapter
where
    I: IntoIterator<Item = &'static str>,
{
    fn do_attach_many(builder: MyBuilder, children: I) -> MyBuilder {
        builder.push_many(children)
    }
}

let built = MyAdapter::attach_many(MyAdapter::attach(MyBuilder::default(), "a"), ["b", "c"]);
assert_eq!(built.0, ["a", "b", "c"]);

Provided Methods§

Source

fn start<B>(builder: B) -> B

Build or wrap the initial builder for a tag. Override when the underlying toolkit requires additional setup on each new element.

Source

fn attach<B, C>(builder: B, child: C) -> B
where Self: CanAttach<B, C>,

Attach a single child to the builder using the adapter-specific glue.

Source

fn attach_many<B, I, C>(builder: B, children: I) -> B
where Self: CanAttachMany<B, I, C>,

Attach an iterator of children to the builder using the adapter-specific glue. Prefer this for spread (..iter) children to avoid intermediate allocations where possible.

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.

Implementors§