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