Currently custom allocators and custom panic runtimes must provide their code to rustc in a different and type-unsafe way.
I suggest that rust should provide the possibility to provide them with for example:
//liballoc
#[providable]
trait Allocator{
fn allocate(size: usize) -> *mut u8;
fn deallocate(ptr: *mut u8);
}
//myalloc
extern crate alloc;
struct MyAllocator;
impl alloc::Allocator for MyAllocator{
// ...
}
//libstd
extern crate alloc;
fn allocate() -> &'static u16{
unsafe{&*alloc::Allocator::allocate(2)}
}
Another example:
//crate a
#[providable]
trait A{
fn new(x: u8) -> Self;
fn foo(&self);
}
//crate b
extern crate a;
struct B(u8);
impl A for B{
fn new(x: u8) -> Self{B(x)}
fn foo(&self){println!("abc {}", self.0)}
}
//crate c
extern crate a;
extern crate b;
trait C{
fn bar(quux: a::A);
}
struct D;
impl C for D{
fn bar(quux: a::A){}
}
fn main(){
let abc: Box<C> = Box::new(D::new()); //object safe
abc.bar(a::A::new(1))
}
Functions:
//crate a
#[providable]
fn print(string: String);
//crate b
extern crate a;
#[provides(a::print)]
fn myprint(string: String){
println!("{}", string);
}
Providables are allowed to have methods which take a self arg. They should be treaten like a trait which can only be implemented by one time.
Edit: changed providable declaration syntax and added per function providables