-
-
Notifications
You must be signed in to change notification settings - Fork 42
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
Proposal: add a #[disjointed_static]
attribute
#81
Comments
I am on board with the single symbol implementation strategy. I would accept a PR for this. Thanks! Nice idea. For the distributed_slice implementation strategy, I think just recommending that someone use For a default value: there is no way to do weak symbols in stable Rust, right? I found Naming:
|
I think I'd lean towards something along the lines of Other potentially useful terms for naming:
Re weak symbols: there's always The main implementation question, then, is whether the linked symbol should be As an aside, note that a single symbol based solution does implicitly rely on attempting to reference an undefined symbol being an error (and not just e.g. dangling into who-knows-where) for soundness. This is always the case for static linking of the symbol as far as I'm aware, but I don't know about the corner cases of dynamic linking. I should have some time to get my sketch functional sometime next week, depending on how other projects go. (I've false started basically this concept multiple times before the thought for it to live in linkme.) |
#[global_resource]
attribute#[disjointed_static]
attribute
Declaring a
static
to be defined downstream seems right in the "linker shenanigans" alley of the linkme crate, and can be made (mostly) typesafe using essentially the same linker techniques as with#[distributed_slice]
.I'm most interested in the
static RESOURCE: dyn Trait
case (c.f.#[distributed_slice]
usingstatic SLICE: [T]
), but any concrete type that can be put in astatic
could of course be used.If you agree that this would be a reasonable fit for the linkme crate to provide, I'm willing to work on getting a working implementation.
Example
Implementation strategy
There are two main potential implementation strategies. The first is to use a
DistributedSlice<&dyn Log>
with a runtimeassert_eq!(len(), 1)
(like the distributed slice dupcheck). Reusing the#[distibuted_slice]
machinery is a reason for putting this in linkme.The second one is to use a single symbol instead, e.g.
(attributes aren't exactly correct). An advantage of this approach is that it (potentially) works under Miri, but a big disadvantage is that it relies on linker errors for missing or duplicated definition of the linked symbol. Using weak symbols might be able to avoid this.
As a variation, instead of linking
static: &dyn Log
, linkfn() -> &dyn Log
instead. That could be more well supported and allows the downstream to more directly do lazy initialization, which could be an upside or drawback, depending on what you want.In either case, the ability to provide a default implementation is useful (e.g. how
#[global_allocator]
defaults to as-ifGlobal
is used) and would theoretically be possible: a strongly ordered slice member for the former implementation strategy, and weak symbol definition for the latter.The text was updated successfully, but these errors were encountered: