• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

facet-rs / facet / 15763210226

19 Jun 2025 05:15PM UTC coverage: 61.09% (+1.4%) from 59.678%
15763210226

push

github

fasterthanlime
Remove ScalarDef

It mostly contains information that we can find through other means,
like the layout size or Type

183 of 387 new or added lines in 10 files covered. (47.29%)

26 existing lines in 4 files now uncovered.

10700 of 17515 relevant lines covered (61.09%)

155.85 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.88
/facet-core/src/impls_alloc/string.rs
1
use crate::{Def, Facet, Shape, Type, UserType, ValueVTable, value_vtable};
2
use alloc::string::ToString;
3

4
#[cfg(feature = "alloc")]
5
unsafe impl Facet<'_> for alloc::string::String {
6
    const VTABLE: &'static ValueVTable = &const {
7
        let mut vtable = value_vtable!(alloc::string::String, |f, _opts| write!(
8
            f,
956✔
9
            "{}",
956✔
10
            Self::SHAPE.type_identifier
956✔
11
        ));
12

13
        // Add a custom parse function for String
14
        {
15
            let vtable_sized = vtable.sized_mut().unwrap();
16
            vtable_sized.parse = || {
244✔
17
                Some(|s, target| {
205✔
18
                    // For String, parsing from a string is just copying the string
19
                    Ok(unsafe { target.put(s.to_string()) })
205✔
20
                })
205✔
21
            };
244✔
22
        }
23

24
        vtable
25
    };
26

27
    const SHAPE: &'static Shape<'static> = &const {
28
        Shape::builder_for_sized::<Self>()
29
            .def(Def::Scalar)
30
            .type_identifier("String")
31
            .ty(Type::User(UserType::Opaque))
32
            .build()
33
    };
34
}
35

36
unsafe impl<'a> Facet<'a> for alloc::borrow::Cow<'a, str> {
37
    const VTABLE: &'static ValueVTable = &const {
38
        value_vtable!(alloc::borrow::Cow<'_, str>, |f, _opts| write!(
39
            f,
14✔
40
            "Cow<'_, str>"
14✔
41
        ))
42
    };
43

44
    const SHAPE: &'static Shape<'static> = &const {
45
        Shape::builder_for_sized::<Self>()
46
            .def(Def::Scalar)
47
            .type_identifier("Cow")
48
            .ty(Type::User(UserType::Opaque))
49
            .build()
50
    };
51
}
52

53
#[cfg(test)]
54
mod tests {
55
    use crate::Facet;
56
    use crate::ptr::PtrUninit;
57
    use alloc::string::String;
58

59
    #[test]
60
    fn test_string_has_parse() {
1✔
61
        // Check that String has a parse function in its vtable
62
        let shape = String::SHAPE;
1✔
63
        assert!(
1✔
64
            shape.vtable.has_parse(),
1✔
NEW
65
            "String should have parse function"
×
66
        );
67
    }
1✔
68

69
    #[test]
70
    fn test_string_parse() {
1✔
71
        // Test that we can parse a string into a String
72
        let shape = String::SHAPE;
1✔
73
        let parse_fn = (shape.vtable.sized().unwrap().parse)().unwrap();
1✔
74

75
        // Allocate memory for the String
76
        let layout = shape.layout.sized_layout().unwrap();
1✔
77
        let ptr = unsafe { alloc::alloc::alloc(layout) };
1✔
78
        let uninit = PtrUninit::new(ptr);
1✔
79

80
        // Parse the string
81
        let result = unsafe { parse_fn("hello world", uninit) };
1✔
82
        assert!(result.is_ok());
1✔
83

84
        // Get the parsed value
85
        let ptr_mut = result.unwrap();
1✔
86
        let parsed = unsafe { ptr_mut.get::<String>() };
1✔
87
        assert_eq!(parsed, &String::from("hello world"));
1✔
88

89
        // Clean up
90
        unsafe {
1✔
91
            ptr_mut.drop_in_place::<String>();
1✔
92
            alloc::alloc::dealloc(ptr, layout);
1✔
93
        }
1✔
94
    }
1✔
95
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc