Skip to content

Commit

Permalink
make Allocator object-safe
Browse files Browse the repository at this point in the history
add test to ensure object-safety
This allows for runtime polymorphic allocators
  • Loading branch information
RustyYato committed Feb 4, 2021
1 parent 120b2a7 commit d06384a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
5 changes: 4 additions & 1 deletion library/core/src/alloc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
13 changes: 13 additions & 0 deletions src/test/ui/allocator/object-safe.rs
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit d06384a

Please sign in to comment.