@@ -131,7 +131,42 @@ impl Layout {
131131 pub fn for_value < T : ?Sized > ( t : & T ) -> Self {
132132 let ( size, align) = ( mem:: size_of_val ( t) , mem:: align_of_val ( t) ) ;
133133 debug_assert ! ( Layout :: from_size_align( size, align) . is_ok( ) ) ;
134- // SAFETY: see rationale in `new` for why this is using an unsafe variant below
134+ // SAFETY: see rationale in `new` for why this is using the unsafe variant
135+ unsafe { Layout :: from_size_align_unchecked ( size, align) }
136+ }
137+
138+ /// Produces layout describing a record that could be used to
139+ /// allocate backing structure for `T` (which could be a trait
140+ /// or other unsized type like a slice).
141+ ///
142+ /// # Safety
143+ ///
144+ /// This function is only safe to call if the following conditions hold:
145+ ///
146+ /// - If `T` is `Sized`, this function is always safe to call.
147+ /// - If the unsized tail of `T` is:
148+ /// - a [slice], then the length of the slice tail must be an intialized
149+ /// integer, and the size of the *entire value*
150+ /// (dynamic tail length + statically sized prefix) must fit in `isize`.
151+ /// - a [trait object], then the vtable part of the pointer must point
152+ /// to a valid vtable for the type `T` acquired by an unsizing coersion,
153+ /// and the size of the *entire value*
154+ /// (dynamic tail length + statically sized prefix) must fit in `isize`.
155+ /// - an (unstable) [extern type], then this function is always safe to
156+ /// call, but may panic or otherwise return the wrong value, as the
157+ /// extern type's layout is not known. This is the same behavior as
158+ /// [`Layout::for_value`] on a reference to an extern type tail.
159+ /// - otherwise, it is conservatively not allowed to call this function.
160+ ///
161+ /// [slice]: ../../std/primitive.slice.html
162+ /// [trait object]: ../../book/ch17-02-trait-objects.html
163+ /// [extern type]: ../../unstable-book/language-features/extern-types.html
164+ #[ unstable( feature = "layout_for_ptr" , issue = "69835" ) ]
165+ pub unsafe fn for_value_raw < T : ?Sized > ( t : * const T ) -> Self {
166+ // SAFETY: we pass along the prerequisites of these functions to the caller
167+ let ( size, align) = unsafe { ( mem:: size_of_val_raw ( t) , mem:: align_of_val_raw ( t) ) } ;
168+ debug_assert ! ( Layout :: from_size_align( size, align) . is_ok( ) ) ;
169+ // SAFETY: see rationale in `new` for why this is using the unsafe variant
135170 unsafe { Layout :: from_size_align_unchecked ( size, align) }
136171 }
137172
0 commit comments