facet_core/impls_alloc/
string.rs

1use crate::{Def, Facet, ScalarAffinity, ScalarDef, Shape, value_vtable};
2
3unsafe impl Facet<'_> for alloc::string::String {
4    const SHAPE: &'static Shape = &const {
5        Shape::builder_for_sized::<Self>()
6            .def(Def::Scalar(
7                ScalarDef::builder()
8                    // `String` is always on the heap
9                    .affinity(ScalarAffinity::string().max_inline_length(0).build())
10                    .build(),
11            ))
12            .vtable(&const { value_vtable!(alloc::string::String, |f, _opts| write!(f, "String")) })
13            .build()
14    };
15}
16
17unsafe impl<'a> Facet<'a> for alloc::borrow::Cow<'a, str> {
18    const SHAPE: &'static Shape = &const {
19        Shape::builder_for_sized::<Self>()
20            .def(Def::Scalar(
21                ScalarDef::builder()
22                    .affinity(ScalarAffinity::string().build())
23                    .build(),
24            ))
25            .vtable(
26                &const {
27                    value_vtable!(alloc::borrow::Cow<'_, str>, |f, _opts| write!(
28                        f,
29                        "Cow<'_, str>"
30                    ))
31                },
32            )
33            .build()
34    };
35}