-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Global resource handling #3
Comments
The perfect general solution need be ML modules, or something else that's just as big a change. But there is a more incremental solution on top of rust-lang/rust#27389 . Basically the pattern there is there is some ZST which implements a trait by calling the methods on the user-defined static. We could replace |
@aturon In our meeting you mentioned event loops are a global resource. Why? If you are currently a task running on an event loop, you should be able to figure that out somehow (e.g. |
Missing from the current allocator API design is a good way to hook into the selected global singleton. There's a lot of compiler magic to make it work for what's tagged See https://github.com/rust-lang/rust/blob/master/src/librustc_allocator/expand.rs and https://github.com/rust-lang/rust/blob/master/src/librustc/middle/allocator.rs I guess the only way to do this kind of dependency inversion in Rust today is with a |
How about leveraging // liballoc
pub mod allocator {
pub unsafe trait Alloc { /* ... */ }
}
pub mod heap {
extern {
#[global="allocator"]
static HEAP: impl ::allocator:Alloc;
}
} // liballoc_system
use alloc::allocator::Alloc;
struct System;
impl Alloc for System { /* ... */ }
#[global="allocator"]
static SYSTEM_ALLOCATOR: System = System; |
Is there an issue using existing/"boring" pub mod heap {
extern {
// Static trait object because an anonymous `impl` type can't be simply linked
static HEAP_IMPL: &::allocator::Alloc;
}
} #[no_mangle]
static HEAP_IMPL: &Alloc = &System; Naively this looks like it would have some runtime overhead from the vtable, but... it's static? I would hope it would be eligible for inlining or LTO or whatever. Not sure if we can do good error reporting if someone includes two global allocators, though. |
I guess that's possible. I'm not a big fan because:
|
Yeah, fair enough. What's your vision on how the compiler should see an |
rust-lang/rfcs#2492 is an RFC for this. |
(@pitdicker I'm sorry to say I didn't include the |
Often, global decisions affecting the entire dependency tree need to be made.
Current examples of this in
std
are panic behavior and the global allocator.Outside of
std
there are logging and event loops, standard RNG.This issue proposes to come up with a generic system that works for all such cases.
The text was updated successfully, but these errors were encountered: