1use alloc::alloc::Layout;
2use alloc::vec::Vec;
3use core::ptr::NonNull;
4
5use alloc::boxed::Box;
6
7use crate::{
8 Def, Facet, KnownPointer, OxPtrMut, PointerDef, PointerFlags, PointerVTable, PtrConst, PtrMut,
9 PtrUninit, Shape, ShapeBuilder, SliceBuilderVTable, TryFromError, Type, TypeNameFn,
10 TypeNameOpts, TypeOpsIndirect, UserType, VTableIndirect,
11};
12
13unsafe fn try_from(
15 src_ptr: PtrConst,
16 src_shape: &'static Shape,
17 dst: PtrUninit,
18) -> Result<PtrMut, TryFromError> {
19 let layout = src_shape.layout.sized_layout().unwrap();
20
21 unsafe {
22 let alloc = alloc::alloc::alloc(layout);
23 if alloc.is_null() {
24 alloc::alloc::handle_alloc_error(layout);
25 }
26
27 let src_ptr = src_ptr.as_ptr::<u8>();
28 core::ptr::copy_nonoverlapping(src_ptr, alloc, layout.size());
29
30 Ok(dst.put(alloc))
32 }
33}
34
35unsafe fn borrow_fn<'a, T: Facet<'a>>(this: PtrConst) -> PtrConst {
37 unsafe {
38 let concrete = this.get::<Box<T>>();
39 let t: &T = concrete.as_ref();
40 PtrConst::new(NonNull::from(t).as_ptr() as *const u8)
41 }
42}
43
44unsafe fn new_into_fn<'a, 'src, T: Facet<'a>>(this: PtrUninit, ptr: PtrMut) -> PtrMut {
46 unsafe { try_from(ptr.as_const(), T::SHAPE, this).unwrap() }
47}
48
49unsafe impl<'a, T: Facet<'a>> Facet<'a> for Box<T> {
51 const SHAPE: &'static crate::Shape = &const {
52 const fn build_type_name<'a, T: Facet<'a>>() -> TypeNameFn {
53 fn type_name_impl<'a, T: Facet<'a>>(
54 _shape: &'static crate::Shape,
55 f: &mut core::fmt::Formatter<'_>,
56 opts: TypeNameOpts,
57 ) -> core::fmt::Result {
58 write!(f, "Box")?;
59 if let Some(opts) = opts.for_children() {
60 write!(f, "<")?;
61 T::SHAPE.write_type_name(f, opts)?;
62 write!(f, ">")?;
63 } else {
64 write!(f, "<…>")?;
65 }
66 Ok(())
67 }
68 type_name_impl::<T>
69 }
70
71 ShapeBuilder::for_sized::<Self>("Box")
72 .type_name(build_type_name::<T>())
73 .vtable_indirect(&VTableIndirect::EMPTY)
74 .type_ops_indirect(
75 &const {
76 unsafe fn drop_in_place<'a, T: Facet<'a>>(ox: OxPtrMut) {
77 unsafe {
78 core::ptr::drop_in_place(ox.ptr().as_ptr::<Box<T>>() as *mut Box<T>)
79 };
80 }
81 TypeOpsIndirect {
82 drop_in_place: drop_in_place::<T>,
83 default_in_place: None,
84 clone_into: None,
85 is_truthy: None,
86 }
87 },
88 )
89 .ty(Type::User(UserType::Opaque))
90 .def(Def::Pointer(PointerDef {
91 vtable: &const {
92 PointerVTable {
93 borrow_fn: Some(borrow_fn::<T>),
94 new_into_fn: Some(new_into_fn::<T>),
95 ..PointerVTable::new()
96 }
97 },
98 pointee: Some(T::SHAPE),
99 weak: None,
100 strong: None,
101 flags: PointerFlags::EMPTY,
102 known: Some(KnownPointer::Box),
103 }))
104 .type_params(&[crate::TypeParam {
105 name: "T",
106 shape: T::SHAPE,
107 }])
108 .inner(T::SHAPE)
109 .variance(Shape::computed_variance)
111 .build()
112 };
113}
114
115unsafe fn box_slice_drop<U>(ox: OxPtrMut) {
121 unsafe { core::ptr::drop_in_place(ox.ptr().as_ptr::<Box<[U]>>() as *mut Box<[U]>) };
122}
123
124unsafe fn box_slice_borrow<'a, U: Facet<'a>>(this: PtrConst) -> PtrConst {
126 unsafe {
127 let concrete = this.get::<Box<[U]>>();
128 let slice: &[U] = concrete.as_ref();
129 PtrConst::new(NonNull::from(slice).as_ptr())
130 }
131}
132
133fn box_slice_builder_new<'a, U: Facet<'a>>() -> PtrMut {
135 let v = Box::new(Vec::<U>::new());
136 let raw = Box::into_raw(v);
137 PtrMut::new(raw as *mut u8)
138}
139
140unsafe fn box_slice_builder_push<'a, U: Facet<'a>>(builder: PtrMut, item: PtrMut) {
141 unsafe {
142 let vec = builder.as_mut::<Vec<U>>();
143 let value = item.read::<U>();
144 vec.push(value);
145 }
146}
147
148unsafe fn box_slice_builder_convert<'a, U: Facet<'a>>(builder: PtrMut) -> PtrConst {
149 unsafe {
150 let vec_box = Box::from_raw(builder.as_ptr::<Vec<U>>() as *mut Vec<U>);
151 let boxed_slice: Box<[U]> = (*vec_box).into_boxed_slice();
152
153 let layout = Layout::new::<Box<[U]>>();
155 let ptr = alloc::alloc::alloc(layout) as *mut Box<[U]>;
156 if ptr.is_null() {
157 alloc::alloc::handle_alloc_error(layout);
158 }
159
160 ptr.write(boxed_slice);
162
163 PtrConst::new(ptr as *const u8)
164 }
165}
166
167unsafe fn box_slice_builder_free<'a, U: Facet<'a>>(builder: PtrMut) {
168 unsafe {
169 let _ = Box::from_raw(builder.as_ptr::<Vec<U>>() as *mut Vec<U>);
170 }
171}
172
173unsafe fn box_str_drop(ox: OxPtrMut) {
179 unsafe { core::ptr::drop_in_place(ox.ptr().as_ptr::<Box<str>>() as *mut Box<str>) };
180}
181
182unsafe fn box_str_borrow(this: PtrConst) -> PtrConst {
184 unsafe {
185 let concrete = this.get::<Box<str>>();
186 let s: &str = concrete;
187 PtrConst::new(NonNull::from(s).as_ptr())
188 }
189}
190
191static BOX_STR_TYPE_OPS: TypeOpsIndirect = TypeOpsIndirect {
193 drop_in_place: box_str_drop,
194 default_in_place: None,
195 clone_into: None,
196 is_truthy: None,
197};
198
199unsafe impl<'a> Facet<'a> for Box<str> {
200 const SHAPE: &'static crate::Shape = &const {
201 fn type_name_box_str(
202 _shape: &'static crate::Shape,
203 f: &mut core::fmt::Formatter<'_>,
204 opts: TypeNameOpts,
205 ) -> core::fmt::Result {
206 write!(f, "Box")?;
207 if let Some(opts) = opts.for_children() {
208 write!(f, "<")?;
209 str::SHAPE.write_type_name(f, opts)?;
210 write!(f, ">")?;
211 } else {
212 write!(f, "<…>")?;
213 }
214 Ok(())
215 }
216
217 ShapeBuilder::for_sized::<Self>("Box")
218 .type_name(type_name_box_str)
219 .vtable_indirect(&VTableIndirect::EMPTY)
220 .type_ops_indirect(&BOX_STR_TYPE_OPS)
221 .ty(Type::User(UserType::Opaque))
222 .def(Def::Pointer(PointerDef {
223 vtable: &const {
224 PointerVTable {
225 borrow_fn: Some(box_str_borrow),
226 ..PointerVTable::new()
227 }
228 },
229 pointee: Some(str::SHAPE),
230 weak: None,
231 strong: None,
232 flags: PointerFlags::EMPTY,
233 known: Some(KnownPointer::Box),
234 }))
235 .type_params(&[crate::TypeParam {
236 name: "T",
237 shape: str::SHAPE,
238 }])
239 .build()
240 };
241}
242
243unsafe impl<'a, U: Facet<'a>> Facet<'a> for Box<[U]> {
244 const SHAPE: &'static crate::Shape = &const {
245 fn type_name_box_slice<'a, U: Facet<'a>>(
246 _shape: &'static crate::Shape,
247 f: &mut core::fmt::Formatter<'_>,
248 opts: TypeNameOpts,
249 ) -> core::fmt::Result {
250 write!(f, "Box")?;
251 if let Some(opts) = opts.for_children() {
252 write!(f, "<")?;
253 <[U]>::SHAPE.write_type_name(f, opts)?;
254 write!(f, ">")?;
255 } else {
256 write!(f, "<…>")?;
257 }
258 Ok(())
259 }
260
261 ShapeBuilder::for_sized::<Self>("Box")
262 .type_name(type_name_box_slice::<U>)
263 .vtable_indirect(&VTableIndirect::EMPTY)
264 .type_ops_indirect(
265 &const {
266 TypeOpsIndirect {
267 drop_in_place: box_slice_drop::<U>,
268 default_in_place: None,
269 clone_into: None,
270 is_truthy: None,
271 }
272 },
273 )
274 .ty(Type::User(UserType::Opaque))
275 .def(Def::Pointer(PointerDef {
276 vtable: &const {
277 PointerVTable {
278 borrow_fn: Some(box_slice_borrow::<U>),
279 slice_builder_vtable: Some(
280 &const {
281 SliceBuilderVTable::new(
282 box_slice_builder_new::<U>,
283 box_slice_builder_push::<U>,
284 box_slice_builder_convert::<U>,
285 box_slice_builder_free::<U>,
286 )
287 },
288 ),
289 ..PointerVTable::new()
290 }
291 },
292 pointee: Some(<[U]>::SHAPE),
293 weak: None,
294 strong: None,
295 flags: PointerFlags::EMPTY,
296 known: Some(KnownPointer::Box),
297 }))
298 .type_params(&[crate::TypeParam {
299 name: "T",
300 shape: <[U]>::SHAPE,
301 }])
302 .build()
303 };
304}
305
306#[cfg(test)]
307mod tests {
308 use core::mem::ManuallyDrop;
309
310 use alloc::boxed::Box;
311 use alloc::string::String;
312
313 use super::*;
314
315 #[test]
316 fn test_box_type_params() {
317 let [type_param_1] = <Box<i32>>::SHAPE.type_params else {
318 panic!("Box<T> should only have 1 type param")
319 };
320 assert_eq!(type_param_1.shape(), i32::SHAPE);
321 }
322
323 #[test]
324 fn test_box_vtable_1_new_borrow_drop() {
325 facet_testhelpers::setup();
326
327 let box_shape = <Box<String>>::SHAPE;
328 let box_def = box_shape
329 .def
330 .into_pointer()
331 .expect("Box<T> should have a smart pointer definition");
332
333 let box_uninit_ptr = box_shape.allocate().unwrap();
335
336 let new_into_fn = box_def
338 .vtable
339 .new_into_fn
340 .expect("Box<T> should have new_into_fn");
341
342 let mut value = ManuallyDrop::new(String::from("example"));
344 let box_ptr = unsafe {
345 new_into_fn(
346 box_uninit_ptr,
347 PtrMut::new(NonNull::from(&mut value).as_ptr()),
348 )
349 };
350 let borrow_fn = box_def
354 .vtable
355 .borrow_fn
356 .expect("Box<T> should have borrow_fn");
357
358 let borrowed_ptr = unsafe { borrow_fn(box_ptr.as_const()) };
360 assert_eq!(unsafe { borrowed_ptr.get::<String>() }, "example");
362
363 unsafe {
366 box_shape
367 .call_drop_in_place(box_ptr)
368 .expect("Box<T> should have drop_in_place");
369 }
370
371 unsafe { box_shape.deallocate_mut(box_ptr).unwrap() };
374 }
375
376 #[test]
377 fn test_box_slice_builder() {
378 facet_testhelpers::setup();
379
380 let box_slice_shape = <Box<[u8]>>::SHAPE;
382 let box_slice_def = box_slice_shape
383 .def
384 .into_pointer()
385 .expect("Box<[u8]> should have a smart pointer definition");
386
387 let slice_builder_vtable = box_slice_def
389 .vtable
390 .slice_builder_vtable
391 .expect("Box<[u8]> should have slice_builder_vtable");
392
393 let builder_ptr = (slice_builder_vtable.new_fn)();
395
396 let push_fn = slice_builder_vtable.push_fn;
398 let values: [u8; 5] = [1, 2, 3, 4, 5];
399 for &value in &values {
400 let mut value_copy = value;
401 let value_ptr = PtrMut::new(NonNull::from(&mut value_copy).as_ptr());
402 unsafe { push_fn(builder_ptr, value_ptr) };
403 }
404
405 let convert_fn = slice_builder_vtable.convert_fn;
407 let box_slice_ptr = unsafe { convert_fn(builder_ptr) };
408
409 let borrow_fn = box_slice_def
411 .vtable
412 .borrow_fn
413 .expect("Box<[u8]> should have borrow_fn");
414 let borrowed_ptr = unsafe { borrow_fn(box_slice_ptr) };
415
416 let slice = unsafe { borrowed_ptr.get::<[u8]>() };
418 assert_eq!(slice, &[1, 2, 3, 4, 5]);
419
420 unsafe {
422 let _ = Box::from_raw(box_slice_ptr.as_ptr::<Box<[u8]>>() as *mut Box<[u8]>);
423 }
424 }
425}