facet_trait/impls/
array_impl.rs

1use crate::*;
2use std::alloc::Layout;
3
4unsafe impl<T> Facet for [T; 1]
5where
6    T: Facet,
7{
8    const DUMMY: Self = [T::DUMMY; 1];
9    const SHAPE: &'static Shape = &const {
10        Shape {
11            layout: Layout::new::<[T; 1]>(),
12            vtable: &ValueVTable {
13                type_name: |f, opts| {
14                    if let Some(opts) = opts.for_children() {
15                        write!(f, "[")?;
16                        (T::SHAPE.vtable.type_name)(f, opts)?;
17                        write!(f, "; 1]")
18                    } else {
19                        write!(f, "[⋯; 1]")
20                    }
21                },
22                display: None,
23                debug: if T::SHAPE.vtable.debug.is_some() {
24                    Some(|value, f| {
25                        let value = unsafe { value.as_ref::<[T; 1]>() };
26                        write!(f, "[")?;
27                        unsafe {
28                            (T::SHAPE.vtable.debug.unwrap_unchecked())(
29                                OpaqueConst::from_ref(&value[0]),
30                                f,
31                            )?;
32                        }
33                        write!(f, "]")
34                    })
35                } else {
36                    None
37                },
38                default_in_place: if T::SHAPE.vtable.default_in_place.is_some() {
39                    Some(|target| unsafe {
40                        let t_dip = T::SHAPE.vtable.default_in_place.unwrap_unchecked();
41                        (t_dip)(target.field_uninit(0))
42                    })
43                } else {
44                    None
45                },
46                clone_into: if T::SHAPE.vtable.clone_into.is_some() {
47                    Some(|src, dst| unsafe {
48                        let t_cip = T::SHAPE.vtable.clone_into.unwrap_unchecked();
49                        (t_cip)(
50                            OpaqueConst::from_ref(&src.as_ref::<[T; 1]>()[0]),
51                            dst.field_uninit(0),
52                        )
53                    })
54                } else {
55                    None
56                },
57                marker_traits: T::SHAPE.vtable.marker_traits,
58                eq: T::SHAPE.vtable.eq,
59                partial_ord: if T::SHAPE.vtable.partial_ord.is_some() {
60                    Some(|a, b| {
61                        let a = unsafe { a.as_ref::<[T; 1]>() };
62                        let b = unsafe { b.as_ref::<[T; 1]>() };
63                        unsafe {
64                            (T::SHAPE.vtable.partial_ord.unwrap_unchecked())(
65                                OpaqueConst::from_ref(&a[0]),
66                                OpaqueConst::from_ref(&b[0]),
67                            )
68                        }
69                    })
70                } else {
71                    None
72                },
73                ord: T::SHAPE.vtable.ord,
74                hash: if T::SHAPE.vtable.hash.is_some() {
75                    Some(|value, state, hasher| {
76                        let value = unsafe { value.as_ref::<[T; 1]>() };
77                        unsafe {
78                            (T::SHAPE.vtable.hash.unwrap_unchecked())(
79                                OpaqueConst::from_ref(&value[0]),
80                                state,
81                                hasher,
82                            )
83                        }
84                    })
85                } else {
86                    None
87                },
88                drop_in_place: Some(|value| unsafe {
89                    std::ptr::drop_in_place(value.as_mut::<[T; 1]>());
90                }),
91                parse: None,
92                try_from: None,
93            },
94            def: Def::List(ListDef {
95                vtable: &ListVTable {
96                    init_in_place_with_capacity: |_, _| Err(()),
97                    push: |_, _| {
98                        panic!("Cannot push to [T; 1]");
99                    },
100                    len: |_| 1,
101                    get_item_ptr: |ptr, index| unsafe {
102                        if index >= 1 {
103                            panic!("Index out of bounds: the len is 1 but the index is {index}");
104                        }
105                        OpaqueConst::new_unchecked(ptr.as_ptr::<[T; 1]>())
106                    },
107                },
108                t: T::SHAPE,
109            }),
110        }
111    };
112}