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 =
8        &const { value_vtable!(alloc::string::String, |f, _opts| write!(f, "String")) };
9
10    const SHAPE: &'static Shape<'static> = &const {
11        Shape::builder_for_sized::<Self>()
12            .def(Def::Scalar(
13                ScalarDef::builder()
14                    // `String` is always on the heap
15                    .affinity(&const { ScalarAffinity::string().max_inline_length(0).build() })
16                    .build(),
17            ))
18            .ty(Type::User(UserType::Opaque))
19            .build()
20    };
21}
22
23unsafe impl<'a> Facet<'a> for alloc::borrow::Cow<'a, str> {
24    const VTABLE: &'static ValueVTable = &const {
25        value_vtable!(alloc::borrow::Cow<'_, str>, |f, _opts| write!(
26            f,
27            "Cow<'_, str>"
28        ))
29    };
30
31    const SHAPE: &'static Shape<'static> = &const {
32        Shape::builder_for_sized::<Self>()
33            .def(Def::Scalar(
34                ScalarDef::builder()
35                    .affinity(&const { ScalarAffinity::string().build() })
36                    .build(),
37            ))
38            .ty(Type::User(UserType::Opaque))
39            .build()
40    };
41}