vy_core/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#![cfg_attr(not(feature = "std"), no_std)]

extern crate alloc;

#[macro_use]
mod helpers;
mod escape;

use alloc::string::String;

pub use escape::*;

/// A type that can be rendered to a [`String`].
///
/// Some types implementing this trait (`&str`, `char`) are escaped by default.
/// To render types unescaped, use [`PreEscaped`].
///
/// [`PreEscaped`]: crate::PreEscaped
pub trait Render {
    fn render_to(self, buf: &mut String);

    #[inline]
    fn render(self) -> String
    where
        Self: Sized,
    {
        let mut buf = String::new();
        self.render_to(&mut buf);
        buf
    }
}

via_itoap! {
    isize i8 i16 i32 i64 i128
    usize u8 u16 u32 u64 u128
}

via_ryu! { f32 f64 }

impl Render for &str {
    #[inline]
    fn render_to(self, buf: &mut String) {
        escape_into(self, buf)
    }
}

impl Render for String {
    #[inline]
    fn render_to(self, buf: &mut String) {
        self.as_str().render_to(buf)
    }
}

impl Render for &String {
    #[inline]
    fn render_to(self, buf: &mut String) {
        self.as_str().render_to(buf)
    }
}

impl Render for char {
    #[inline]
    fn render_to(self, buf: &mut String) {
        escape_into(self.encode_utf8(&mut [0; 4]), buf);
    }
}

impl Render for bool {
    #[inline]
    fn render_to(self, buf: &mut String) {
        buf.push_str(if self { "true" } else { "false" })
    }
}

impl<F: FnOnce(&mut String)> Render for F {
    #[inline]
    fn render_to(self, buf: &mut String) {
        (self)(buf)
    }
}

impl<T: Render> Render for Option<T> {
    #[inline]
    fn render_to(self, buf: &mut String) {
        if let Some(x) = self {
            x.render_to(buf)
        }
    }
}

impl<T: Render, const N: usize> Render for [T; N] {
    #[inline]
    fn render_to(self, buf: &mut String) {
        for x in self {
            x.render_to(buf)
        }
    }
}

impl<T, I: Iterator, F> Render for core::iter::Map<I, F>
where
    T: Render,
    F: FnMut(I::Item) -> T,
{
    #[inline]
    fn render_to(self, buf: &mut String) {
        for x in self {
            x.render_to(buf)
        }
    }
}

impl<T: Render> Render for alloc::vec::IntoIter<T> {
    #[inline]
    fn render_to(self, buf: &mut String) {
        for x in self {
            x.render_to(buf)
        }
    }
}