facet_core/impls_alloc/
string.rs

1use crate::{
2    Def, Facet, ScalarAffinity, ScalarDef, Shape, Type, UserType, ValueVTable, value_vtable,
3};
4
5#[cfg(feature = "alloc")]
6unsafe impl Facet<'_> for alloc::string::String {
7    const VTABLE: &'static ValueVTable = &const {
8        value_vtable!(alloc::string::String, |f, _opts| write!(
9            f,
10            "{}",
11            Self::SHAPE.type_identifier
12        ))
13    };
14
15    const SHAPE: &'static Shape<'static> = &const {
16        Shape::builder_for_sized::<Self>()
17            .def(Def::Scalar(
18                ScalarDef::builder()
19                    // `String` is always on the heap
20                    .affinity(&const { ScalarAffinity::string().max_inline_length(0).build() })
21                    .build(),
22            ))
23            .type_identifier("String")
24            .ty(Type::User(UserType::Opaque))
25            .build()
26    };
27}
28
29unsafe impl<'a> Facet<'a> for alloc::borrow::Cow<'a, str> {
30    const VTABLE: &'static ValueVTable = &const {
31        value_vtable!(alloc::borrow::Cow<'_, str>, |f, _opts| write!(
32            f,
33            "Cow<'_, str>"
34        ))
35    };
36
37    const SHAPE: &'static Shape<'static> = &const {
38        Shape::builder_for_sized::<Self>()
39            .def(Def::Scalar(
40                ScalarDef::builder()
41                    .affinity(&const { ScalarAffinity::string().build() })
42                    .build(),
43            ))
44            .type_identifier("Cow")
45            .ty(Type::User(UserType::Opaque))
46            .build()
47    };
48}