1- //! In-place initialization
1+ //! In-place initialization.
22//!
33//! This module describes the interface through which types supporting in-place initialization
4- //! interact with allocation mechanism to ensure correct and safe initialization of values
5- //! within the memory slots provided by the allocation.
4+ //! can perform initialization with minimal or zero additional allocations or moves.
65
76use crate :: ptr:: Pointee ;
87
8+ /// In-place Initializer for `T`.
9+ ///
10+ /// An instance of `Init<T>` carries all the information necessary to initialize a `T` in a
11+ /// well-defined memory location, criteria of which is prescribed in the Safety section.
12+ ///
13+ /// # Fallibility
14+ ///
15+ /// The initialization might fail and return an error of type [`Self::Error`] instead.
16+ /// In that case, the memory provided to [`Self::init`] shall be repurposed in any way,
17+ /// even though it might have been written to by this initializer.
18+ ///
19+ /// # Examples
20+ ///
21+ /// ## Initializing unsized types
22+ ///
23+ /// To initialize an unsized type, first query the required layout for `T` using [`Self::layout`].
24+ /// Then provide a pointer to an allocation of at least the specified alignment and size.
25+ ///
26+ /// If initialization was successful, then [`Self::init`] returns the metadata that combined with
27+ /// the pointer to the given to [`Self::init`] yields a valid pointer to `T`.
28+ ///
29+ /// ```
30+ /// use std::alloc::alloc;
31+ /// fn init_unsized<T: ?Sized + Pointee, I: Init<T>>(init: I) -> Result<Box<T>, I::Error> {
32+ /// let layout = init.layout();
33+ /// let memory = alloc(layout).cast::<()>();
34+ /// let meta = init.init(memory)?;
35+ /// Box::from_raw(from_raw_parts_mut(memory, meta))
36+ /// }
37+ /// ```
38+ ///
939/// # Safety
1040///
11- /// Implementers must ensure that if `init` returns `Ok(metadata)`, then
12- /// `core::ptr::from_raw_parts_mut(slot, metadata)` must reference a valid
13- /// value owned by the caller. Furthermore, the layout returned by using
14- /// `size_of` and `align_of` on this pointer must match what `Self::layout()`
15- /// returns exactly.
41+ /// Implementers must ensure that if [`self.init(slot)`] returns `Ok(metadata)`,
42+ /// then [`core::ptr::from_raw_parts_mut(slot, metadata)`] must reference a valid
43+ /// value owned by the caller.
44+ /// Furthermore, the layout returned by using
45+ /// [`core::intrinsics::size_of_val`] and [`core::intrinsics::align_of_val`] on this pointer
46+ /// must match what [`Self::layout()`] returns exactly.
47+ ///
48+ /// If `T` is sized, or in other words `T: Sized`, [`Init::layout`] in this case *must*
49+ /// return the same layout as [`Layout::new::<T>()`] would.
1650///
1751/// Implementers must ensure that the implementation of `init()` does not rely
1852/// on the value being pinned.
53+ ///
54+ /// [`core::ptr::from_raw_parts_mut(slot, metadata)`]: core::ptr::from_raw_parts_mut
55+ /// [`Self::layout()`]: Init::layout
56+ /// [`self.init(slot)`]: Init::init
57+ /// [`Layout::new::<T>()`]: core::alloc::Layout::new
1958#[ unstable( feature = "in_place_initialization" , issue = "999999" ) ]
2059#[ lang = "init_trait" ]
2160pub unsafe trait Init < T : ?Sized + Pointee > {
@@ -24,8 +63,7 @@ pub unsafe trait Init<T: ?Sized + Pointee> {
2463 #[ lang = "init_error" ]
2564 type Error ;
2665
27- /// The layout needed by this initializer, which the allocation
28- /// should arrange the destination memory slot accordingly.
66+ /// The layout needed by this initializer.
2967 #[ lang = "init_layout" ]
3068 fn layout ( & self ) -> crate :: alloc:: Layout ;
3169
@@ -43,10 +81,6 @@ pub unsafe trait Init<T: ?Sized + Pointee> {
4381 /// The caller must provide a pointer that references a location that `init`
4482 /// may write to, and the location must have at least the size and alignment
4583 /// specified by [`Init::layout`].
46- ///
47- /// If this call returns `Ok` and the initializer does not implement
48- /// `Init<T>`, then `slot` contains a pinned value, and the caller must
49- /// respect the usual pinning requirements for `slot`.
5084 #[ lang = "init_init" ]
5185 unsafe fn init ( self , slot : * mut ( ) ) -> Result < T :: Metadata , Self :: Error > ;
5286}
0 commit comments