Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add trait implementations for Vec<T>, [T], Option<T> #421

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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
26 changes: 25 additions & 1 deletion maud/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

extern crate alloc;

use alloc::{borrow::Cow, boxed::Box, string::String, sync::Arc};
use alloc::{borrow::Cow, boxed::Box, string::String, sync::Arc, vec::Vec};
use core::fmt::{self, Arguments, Display, Write};

pub use maud_macros::html;
Expand Down Expand Up @@ -156,6 +156,30 @@ impl<T: Render + ?Sized> Render for Arc<T> {
}
}

impl<T: Render> Render for Vec<T> {
fn render_to(&self, w: &mut String) {
for item in self {
item.render_to(w);
}
}
}

impl<T: Render> Render for [T] {
fn render_to(&self, w: &mut String) {
for item in self {
item.render_to(w);
}
}
}

impl<T: Render> Render for Option<T> {
fn render_to(&self, w: &mut String) {
if let Some(ref value) = *self {
value.render_to(w);
}
}
}

macro_rules! impl_render_with_display {
($($ty:ty)*) => {
$(
Expand Down
34 changes: 33 additions & 1 deletion maud/tests/misc.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use maud::html;
use maud::{html, Render};

#[test]
fn issue_13() {
Expand Down Expand Up @@ -143,3 +143,35 @@ fn render_arc() {
let arc = std::sync::Arc::new("foo");
assert_eq!(html! { (arc) }.into_string(), "foo");
}

#[test]
fn render_vec_generics() {
let vec = vec![1, 2, 3];
assert_eq!(vec.render().into_string(), "123");
let vec = vec!["a", "b", "c"];
assert_eq!(vec.render().into_string(), "abc");
let vec = vec![String::from("a"), String::from("b"), String::from("c")];
assert_eq!(vec.render().into_string(), "abc");
}

#[test]
fn render_slice_generics() {
let slice = &[1, 2, 3];
assert_eq!(slice.render().into_string(), "123");
let slice = &["a", "b", "c"];
assert_eq!(slice.render().into_string(), "abc");
let slice = &[String::from("a"), String::from("b"), String::from("c")];
assert_eq!(slice.render().into_string(), "abc");
}

#[test]
fn render_option_generics() {
let option = Some(42);
assert_eq!(option.render().into_string(), "42");
let option = Some("foo");
assert_eq!(option.render().into_string(), "foo");
let option = Some(String::from("bar"));
assert_eq!(option.render().into_string(), "bar");
let option: Option<String> = None;
assert_eq!(option.render().into_string(), "");
}
Loading