More specifically, a confusing but ever-so-common construct is:
if (!self.doneonce) {
let x = get_substring(self.x);
println!("{} {} whatever", self.a, x);
}
and it's bad for many reasons:
maybe you'll get warnings about unused struct fields or w/e but I'd much rather be able to just write:
self.logged.call_once(|| {
let x = get_substring(self.x);
println!("{} {} whatever", self.a, x);
});
and I can't do that with the standard Once. the standard Once is not standard enough - instead, it's too niche.
so, can we get nice Once? all it takes is to remove an 'static lifetime bounds.
I'm assuming that you are referring to std::sync::Once. So it is normally used to initialise static variables, which can only have the lifetime 'static, which is why the function has that signature, because it doesn't make sense for any other lifetime which is shorter than the duration of the program. You really shouldn't be using Once for anything else
I'm assuming that you are referring to
std::sync::Once.
yes.
So it is normally used to initialise
staticvariables, which can only have the lifetime'static, which is why the function has that signature, because it doesn't make sense for any other lifetime which is shorter than the duration of the program.
yes, but by relaxing that requirement, it can still be used with statics.
You really shouldn't be using
Oncefor anything else
why not?
I agree, there's no good reason for Once to require 'static. The version in parking_lot does not require 'static.
I would like to also mention [spin-rs] doesnt have this problem either. with my limited knowledge of lock free code, I looked at std::Once::Once,
I dont believe that &'static self is strictly required for a safe implementation.
an example use case where you would want to use once is to is to create a thunk library.
a structure, that lazy generates a expression. using std::Once:once we can guarantee, that expression
is only ran once but the thunk library would be practically useless with static life time bound because,
thunks are expected to be create runtime, that only execute closure on first access.
the closure enviroment could be created dynamically which should have no affect on thunk implementation.
thunks because they are simply lazy expressions, are not to be used for statics
The 'static bound of std::sync::Once has been removed in rust-lang/rust#52239 and would be available since 1.29.
This issue has been fixed in master (see above), and can be closed, I think.