make Allocator object-safe

add test to ensure object-safety
This allows for runtime polymorphic allocators
This commit is contained in:
RustyYato 2021-02-03 16:59:28 -05:00
parent 120b2a704a
commit d06384ac29
No known key found for this signature in database
GPG Key ID: 8F93A3A7AA801EDB
2 changed files with 17 additions and 1 deletions

View File

@ -342,7 +342,10 @@ pub unsafe trait Allocator {
///
/// The returned adaptor also implements `Allocator` and will simply borrow this.
#[inline(always)]
fn by_ref(&self) -> &Self {
fn by_ref(&self) -> &Self
where
Self: Sized,
{
self
}
}

View File

@ -0,0 +1,13 @@
// run-pass
// Check that `Allocator` is object safe, this allows for polymorphic allocators
#![feature(allocator_api)]
use std::alloc::{Allocator, System};
fn ensure_object_safe(_: &dyn Allocator) {}
fn main() {
ensure_object_safe(&System);
}