facet_core/impls_core/
ops.rs

1use crate::{ConstTypeId, Facet, Field, Shape, StructType, Type, VTableView, ValueVTable};
2use core::{alloc::Layout, mem};
3
4unsafe impl<'a, Idx: Facet<'a>> Facet<'a> for core::ops::Range<Idx> {
5    const SHAPE: &'static crate::Shape = &const {
6        Shape::builder_for_sized::<Self>()
7            .type_params(&[crate::TypeParam {
8                name: "Idx",
9                shape: || Idx::SHAPE,
10            }])
11            .id(ConstTypeId::of::<Self>())
12            .layout(Layout::new::<Self>())
13            .ty(Type::User(crate::UserType::Struct(
14                StructType::builder()
15                    .kind(crate::StructKind::Struct)
16                    .repr(crate::Repr::default())
17                    .fields(
18                        &const {
19                            [
20                                Field::builder()
21                                    .name("start")
22                                    .shape(Idx::SHAPE)
23                                    .offset(mem::offset_of!(core::ops::Range<Idx>, start))
24                                    .build(),
25                                Field::builder()
26                                    .name("end")
27                                    .shape(Idx::SHAPE)
28                                    .offset(mem::offset_of!(core::ops::Range<Idx>, end))
29                                    .build(),
30                            ]
31                        },
32                    )
33                    .build(),
34            )))
35            .build()
36    };
37
38    const VTABLE: &'static ValueVTable = &const {
39        let mut builder = ValueVTable::builder::<Self>().type_name(|f, opts| {
40            write!(f, "Range")?;
41            if let Some(opts) = opts.for_children() {
42                write!(f, "<")?;
43                (Idx::SHAPE.vtable.type_name)(f, opts)?;
44                write!(f, ">")?;
45            } else {
46                write!(f, "<…>")?;
47            }
48            Ok(())
49        });
50
51        if Idx::SHAPE.is_debug() {
52            builder = builder.debug(|this, f| {
53                (<VTableView<Idx>>::of().debug().unwrap())(&this.start, f)?;
54                write!(f, "..")?;
55                (<VTableView<Idx>>::of().debug().unwrap())(&this.end, f)?;
56                Ok(())
57            });
58        }
59
60        builder.build()
61    };
62}