facet_core/impls_core/
tuple.rs1use core::{fmt, mem};
2
3use crate::{
4 Characteristic, Facet, MarkerTraits, Repr, Shape, StructKind, StructType, Type, TypeNameOpts,
5 UserType, VTableView, ValueVTable, types::field_in_type,
6};
7
8#[inline(always)]
9pub fn write_type_name_list(
10 f: &mut fmt::Formatter<'_>,
11 opts: TypeNameOpts,
12 open: &'static str,
13 delimiter: &'static str,
14 close: &'static str,
15 shapes: &'static [&'static Shape],
16) -> fmt::Result {
17 f.pad(open)?;
18 if let Some(opts) = opts.for_children() {
19 for (index, shape) in shapes.iter().enumerate() {
20 if index > 0 {
21 f.pad(delimiter)?;
22 }
23 shape.write_type_name(f, opts)?;
24 }
25 } else {
26 write!(f, "…")?;
27 }
28 f.pad(close)?;
29 Ok(())
30}
31
32macro_rules! impl_facet_for_tuple {
33 {
36 continue from ($($elems:ident.$idx:tt,)+),
37 remaining ()
38 } => {};
39 {
40 continue from ($($elems:ident.$idx:tt,)+),
41 remaining ($next:ident.$nextidx:tt, $($remaining:ident.$remainingidx:tt,)*)
42 } => {
43 impl_facet_for_tuple! {
44 impl ($($elems.$idx,)+ $next.$nextidx,),
45 remaining ($($remaining.$remainingidx,)*)
46 }
47 };
48 { debug on $f:ident { $first:stmt; } } => {
50 write!($f, "(")?;
51 $first
52 write!($f, ",)")
53 };
54 {
56 impl ($($elems:ident.$idx:tt,)+),
57 remaining ($($remaining:ident.$remainingidx:tt,)*)
58 } => {
59 unsafe impl<'a $(, $elems)+> Facet<'a> for ($($elems,)+)
60 where
61 $($elems: Facet<'a>,)+
62 {
63
64 const SHAPE: &'static Shape = &const {
65 Shape::builder_for_sized::<Self>()
66 .vtable(
67 ValueVTable::builder::<Self>()
68 .type_name(|f, opts| {
69 write_type_name_list(f, opts, "(", ", ", ")", &[$($elems::SHAPE),+])
70 })
71 .drop_in_place(Some(|data| unsafe { data.drop_in_place::<Self>() }))
72 .marker_traits( MarkerTraits::all()
73 $(.intersection($elems::SHAPE.vtable.marker_traits()))+
74 )
75 .default_in_place({
76 let elem_shapes = const { &[$($elems::SHAPE),+] };
77 if Characteristic::all_default(elem_shapes) {
78 Some(|mut dst| {
79 $(
80 unsafe {
81 (<VTableView<$elems>>::of().default_in_place().unwrap())(
82 dst.field_uninit_at(mem::offset_of!(Self, $idx))
83 );
84 }
85 )+
86
87 unsafe { dst.assume_init().into() }
88 })
89 } else {
90 None
91 }
92 })
93 .build()
94 )
95 .type_identifier(const {
96 let fields = [
97 $(field_in_type!(Self, $idx),)+
98 ];
99 if fields.len() == 1 {
100 "(_)"
101 } else {
102 "(…)"
103 }
104 })
105 .ty(Type::User(UserType::Struct(StructType {
106 repr: Repr::default(),
107 kind: StructKind::Tuple,
108 fields: &const {[
109 $(field_in_type!(Self, $idx),)+
110 ]}
111 })))
112 .build()
113 };
114 }
115
116 impl_facet_for_tuple! {
117 continue from ($($elems.$idx,)+),
118 remaining ($($remaining.$remainingidx,)*)
119 }
120 };
121 { ($first:ident.$firstidx:tt $(, $remaining:ident.$remainingidx:tt)* $(,)?) } => {
123 impl_facet_for_tuple! {
124 impl ($first.$firstidx,),
125 remaining ($($remaining.$remainingidx,)*)
126 }
127 };
128}
129
130#[cfg(feature = "tuples-12")]
131impl_facet_for_tuple! {
132 (T0.0, T1.1, T2.2, T3.3, T4.4, T5.5, T6.6, T7.7, T8.8, T9.9, T10.10, T11.11)
133}
134
135#[cfg(not(feature = "tuples-12"))]
136impl_facet_for_tuple! {
137 (T0.0, T1.1, T2.2, T3.3)
138}