-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Different static variables for different instances of a generic function #2130
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
Comments
FWIW the Rust semantics are "if you can write outside a function, putting it inside a block expression will only change where you can refer to it by name and nothing else". So you can embed entire modules in an expression - not only in functions but also array lengths, and they behave the same as outside, except name resolution can get a bit confusing in some cases. |
I'd imagine you want polymorphic/generic
Also, the polymorphic/generic
I wonder if this could be merged with #1945 but even if not that might give you ideas about what the RFC should look like. |
@eddyb Okay, I agree that if we treat fn foo1() {
static A: i64 = 1i64;
println!("{}", A);
}
fn foo2() {
static A: i64 = 2i64;
println!("{}", A);
} , where two @burdges Generic statics indeed seems to solve my problem as you wrote although I have no idea what should happen in the following example: fn foo<T, S>() {
...
struct OnceAndPhandom<T>(Once,PhandomData<T>);
static INIT: OnceAndPhandom<T> = (ONCE_INIT,PhandomData);
INIT.0.call_once(|| {
...
}
fn main() {
foo<i64, usize>();
foo<i64, f64>();
} |
This sounds like https://github.com/chris-morgan/anymap |
All you have to do for consistency is to move anything that can be written outside a function, outside. |
@hajifkd: Can this issue be closed now? |
Okay. I'm sorry to bother you. |
AFAICT the lack of this feature means size specific global free lists in |
I know this is many years later but is there an open issue for polymorphic statics? Im unable to find one. cc @burdges |
Are there any existing places, or if it is possible at all, that static data that can be dynamically created and still shared between dependent crates without a single crate that owns the static field? |
Also I don't know why you have a requirement to use statics on an arbitrary type in the first place, but if all such types requiring such a static field are known at a confined point, you can simply make it a trait: #[derive(HasStatic)]
#[static(StaticType = init_expr)]
struct Foo;
// derives
const _: () = {
static _STATIC: StaticType = init_expr;
impl HasStatic<StaticType> for Foo {
fn get(&self) -> &StaticType { &_STATIC }
}
}; |
Currently, different instances of a generic function have an identical static variable.
For example, the following code
calls
println!
just once. This is because the entities of the static variableINIT
are the same for each different instance of the generic functionfoo
.(Well, actually I have asked this in stackoverflow, but I only got a workaround to use HashMap essentially. Using dynamical way to dispatch static code sounds a bit uncomfortable. )
I feel this behavior of generic functions and static variables is counter-intuitive since each instances of generic functions have different name in assembly and thus they are different function.
Of course, I agree that changing it loses compatibility and may not be good, so I would like to ask whether there is any room to add a new syntax or something to allow for different instances of a generic function to have different static variables. I feel this is important especially when we "translate" some C++ template code into Rust.
The text was updated successfully, but these errors were encountered: