Struct Safe

Source
pub struct Safe<T>(pub T);
Expand description

Mark the output of a filter as “safe”

This struct can be used as a transparent return type of custom filters that want to mark their output as “safe” no matter what, i.e. that their output does not need to be escaped.

If the filter is not used as the last element in the filter chain, then any assumption is void. Let the next filter decide if the output is safe or not.

§Example

mod filters {
    use askama::{filters::Safe, Result};

    // Do not actually use this filter! It's an intentionally bad example.
    pub fn strip_except_apos(s: impl ToString) -> Result<Safe<String>> {
        Ok(Safe(s
            .to_string()
            .chars()
            .filter(|c| !matches!(c, '<' | '>' | '"' | '&'))
            .collect()
        ))
    }
}

#[derive(askama::Template)]
#[template(
    source = "<div class='{{ klass|strip_except_apos }}'></div>",
    ext = "html"
)]
struct DivWithClass<'a> {
    klass: &'a str,
}

assert_eq!(
    DivWithClass { klass: "<&'lifetime X>" }.to_string(),
    "<div class=''lifetime X'></div>",
);

Tuple Fields§

§0: T

Trait Implementations§

Source§

impl<T: Display> Display for Safe<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: FastWritable> FastWritable for Safe<T>

Source§

fn write_into<W: Write + ?Sized>(&self, dest: &mut W) -> Result<()>

Used internally by askama to speed up writing some types.

Auto Trait Implementations§

§

impl<T> Freeze for Safe<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Safe<T>
where T: RefUnwindSafe,

§

impl<T> Send for Safe<T>
where T: Send,

§

impl<T> Sync for Safe<T>
where T: Sync,

§

impl<T> Unpin for Safe<T>
where T: Unpin,

§

impl<T> UnwindSafe for Safe<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.