@@ -31,7 +31,7 @@ const fn size_align<T>() -> (usize, usize) {
3131///
3232/// (Note however that layouts are *not* required to have positive
3333/// size, even though many allocators require that all memory
34- /// requests have positive size. A caller to the `Alloc ::alloc`
34+ /// requests have positive size. A caller to the `AllocRef ::alloc`
3535/// method must either ensure that conditions like this are met, or
3636/// use specific allocators with looser requirements.)
3737#[ stable( feature = "alloc_layout" , since = "1.28.0" ) ]
@@ -364,8 +364,8 @@ impl fmt::Display for AllocErr {
364364/// [`shrink_in_place`] were unable to reuse the given memory block for
365365/// a requested layout.
366366///
367- /// [`grow_in_place`]: ./trait.Alloc .html#method.grow_in_place
368- /// [`shrink_in_place`]: ./trait.Alloc .html#method.shrink_in_place
367+ /// [`grow_in_place`]: ./trait.AllocRef .html#method.grow_in_place
368+ /// [`shrink_in_place`]: ./trait.AllocRef .html#method.shrink_in_place
369369#[ unstable( feature = "allocator_api" , issue = "32838" ) ]
370370#[ derive( Clone , PartialEq , Eq , Debug ) ]
371371pub struct CannotReallocInPlace ;
@@ -580,9 +580,14 @@ pub unsafe trait GlobalAlloc {
580580 }
581581}
582582
583- /// An implementation of `Alloc ` can allocate, reallocate, and
583+ /// An implementation of `AllocRef ` can allocate, reallocate, and
584584/// deallocate arbitrary blocks of data described via `Layout`.
585585///
586+ /// `AllocRef` is designed to be implemented on ZSTs, references, or
587+ /// smart pointers because having an allocator like `MyAlloc([u8; N])`
588+ /// cannot be moved, without updating the pointers to the allocated
589+ /// memory.
590+ ///
586591/// Some of the methods require that a memory block be *currently
587592/// allocated* via an allocator. This means that:
588593///
@@ -598,15 +603,15 @@ pub unsafe trait GlobalAlloc {
598603/// passed to a reallocation method (see above) that returns `Ok`.
599604///
600605/// A note regarding zero-sized types and zero-sized layouts: many
601- /// methods in the `Alloc ` trait state that allocation requests
606+ /// methods in the `AllocRef ` trait state that allocation requests
602607/// must be non-zero size, or else undefined behavior can result.
603608///
604609/// * However, some higher-level allocation methods (`alloc_one`,
605610/// `alloc_array`) are well-defined on zero-sized types and can
606611/// optionally support them: it is left up to the implementor
607612/// whether to return `Err`, or to return `Ok` with some pointer.
608613///
609- /// * If an `Alloc ` implementation chooses to return `Ok` in this
614+ /// * If an `AllocRef ` implementation chooses to return `Ok` in this
610615/// case (i.e., the pointer denotes a zero-sized inaccessible block)
611616/// then that returned pointer must be considered "currently
612617/// allocated". On such an allocator, *all* methods that take
@@ -646,21 +651,24 @@ pub unsafe trait GlobalAlloc {
646651///
647652/// # Safety
648653///
649- /// The `Alloc ` trait is an `unsafe` trait for a number of reasons, and
654+ /// The `AllocRef ` trait is an `unsafe` trait for a number of reasons, and
650655/// implementors must ensure that they adhere to these contracts:
651656///
652657/// * Pointers returned from allocation functions must point to valid memory and
653- /// retain their validity until at least the instance of `Alloc ` is dropped
658+ /// retain their validity until at least one instance of `AllocRef ` is dropped
654659/// itself.
655660///
661+ /// * Cloning or moving the allocator must not invalidate pointers returned
662+ /// from this allocator. Cloning must return a reference to the same allocator.
663+ ///
656664/// * `Layout` queries and calculations in general must be correct. Callers of
657665/// this trait are allowed to rely on the contracts defined on each method,
658666/// and implementors must ensure such contracts remain true.
659667///
660668/// Note that this list may get tweaked over time as clarifications are made in
661669/// the future.
662670#[ unstable( feature = "allocator_api" , issue = "32838" ) ]
663- pub unsafe trait Alloc {
671+ pub unsafe trait AllocRef {
664672 // (Note: some existing allocators have unspecified but well-defined
665673 // behavior in response to a zero size allocation request ;
666674 // e.g., in C, `malloc` of 0 will either return a null pointer or a
@@ -1042,7 +1050,7 @@ pub unsafe trait Alloc {
10421050 /// must be considered "currently allocated" and must be
10431051 /// acceptable input to methods such as `realloc` or `dealloc`,
10441052 /// *even if* `T` is a zero-sized type. In other words, if your
1045- /// `Alloc ` implementation overrides this method in a manner
1053+ /// `AllocRef ` implementation overrides this method in a manner
10461054 /// that can return a zero-sized `ptr`, then all reallocation and
10471055 /// deallocation methods need to be similarly overridden to accept
10481056 /// such values as input.
@@ -1106,7 +1114,7 @@ pub unsafe trait Alloc {
11061114 /// must be considered "currently allocated" and must be
11071115 /// acceptable input to methods such as `realloc` or `dealloc`,
11081116 /// *even if* `T` is a zero-sized type. In other words, if your
1109- /// `Alloc ` implementation overrides this method in a manner
1117+ /// `AllocRef ` implementation overrides this method in a manner
11101118 /// that can return a zero-sized `ptr`, then all reallocation and
11111119 /// deallocation methods need to be similarly overridden to accept
11121120 /// such values as input.
@@ -1219,3 +1227,10 @@ pub unsafe trait Alloc {
12191227 }
12201228 }
12211229}
1230+
1231+ // In order to rename `Alloc` to `AllocRef`, some submoduleshas to be updated as well. The CI fails
1232+ // if either of the submodules fails to compile. The submodules have their own CI depending on a
1233+ // specific Rust version, which don't have `AllocRef` yet. This alias is used to make the submodules
1234+ // compile and pass the CI.
1235+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
1236+ pub use self :: AllocRef as Alloc ;
0 commit comments