Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions hypertext/src/alloc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,13 @@ impl<F: Fn(&mut Buffer<C>), C: Context> Debug for Lazy<F, C> {
}
}

impl<C: Context> Default for Lazy<fn(&mut Buffer<C>), C> {
#[inline]
fn default() -> Lazy<fn(&mut Buffer<C>), C> {
Lazy::dangerously_create(|_| ())
}
}

/// A value rendered via its [`Display`] implementation.
///
/// This will handle escaping special characters for you.
Expand Down
45 changes: 44 additions & 1 deletion hypertext/tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use std::fmt::{self, Display, Formatter};

use hypertext::{Buffer, Raw, maud_borrow, maud_static, prelude::*, rsx_borrow, rsx_static};
use hypertext::{Buffer, Lazy, Raw, maud_borrow, maud_static, prelude::*, rsx_borrow, rsx_static};

#[test]
fn readme() {
Expand Down Expand Up @@ -749,3 +749,46 @@ fn toggles() {
);
assert_eq!(rsx_result.as_inner(), r#"<input type="checkbox" checked>"#);
}

#[test]
fn derive_default() {
#[derive(Default)]
struct Element<'a> {
pub id: &'a str,
pub tabindex: u32,
pub children: Lazy<fn(&mut Buffer)>,
}

impl<'a> Renderable for Element<'a> {
fn render_to(&self, buf: &mut Buffer) {
rsx! {
<div id=(self.id) tabindex=(self.tabindex)>
(self.children)
</div>
}
.render_to(buf)
}
}

let with_children = rsx! {
<Element ..>
<h1>hello</h1>
</Element>
}
.render();

assert_eq!(
with_children.as_inner(),
r#"<div id="" tabindex="0"><h1>hello</h1></div>"#
);

let without_children = rsx! {
<Element ../>
}
.render();

assert_eq!(
without_children.as_inner(),
r#"<div id="" tabindex="0"></div>"#
);
}
Loading