tachys/view/add_attr.rs
1use super::RenderHtml;
2use crate::html::attribute::Attribute;
3
4/// Allows adding a new attribute to some type, before it is rendered.
5/// This takes place at compile time as part of the builder syntax for creating a statically typed
6/// view tree.
7///
8/// Normally, this is used to add an attribute to an HTML element. But it is required to be
9/// implemented for all types that implement [`RenderHtml`], so that attributes can be spread onto
10/// other structures like the return type of a component.
11pub trait AddAnyAttr {
12 /// The new type once the attribute has been added.
13 type Output<SomeNewAttr: Attribute>: RenderHtml;
14
15 /// Adds an attribute to the view.
16 fn add_any_attr<NewAttr: Attribute>(
17 self,
18 attr: NewAttr,
19 ) -> Self::Output<NewAttr>
20 where
21 Self::Output<NewAttr>: RenderHtml;
22}
23
24/// Declares that spreading attributes onto a particular type has no effect.
25#[macro_export]
26macro_rules! no_attrs {
27 ($ty_name:ty) => {
28 impl<'a> $crate::view::add_attr::AddAnyAttr for $ty_name {
29 type Output<SomeNewAttr: $crate::html::attribute::Attribute> =
30 $ty_name;
31
32 fn add_any_attr<NewAttr: $crate::html::attribute::Attribute>(
33 self,
34 _attr: NewAttr,
35 ) -> Self::Output<NewAttr> {
36 self
37 }
38 }
39 };
40}