Skip to content
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

Relax Once so I can use it for arbitrary things that should run once. #2467

Closed
SoniEx2 opened this issue Jun 9, 2018 · 6 comments
Closed

Comments

@SoniEx2
Copy link

SoniEx2 commented Jun 9, 2018

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:

  1. completely unclear variable names.
  2. the example I showed has a bug.

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.

@shingtaklam1324
Copy link

shingtaklam1324 commented Jun 10, 2018

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

@SoniEx2
Copy link
Author

SoniEx2 commented Jun 10, 2018

I'm assuming that you are referring to std::sync::Once.

yes.

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.

yes, but by relaxing that requirement, it can still be used with statics.

You really shouldn't be using Once for anything else

why not?

@Amanieu
Copy link
Member

Amanieu commented Jun 10, 2018

I agree, there's no good reason for Once to require 'static. The version in parking_lot does not require 'static.

@warlord500
Copy link

warlord500 commented Jun 10, 2018

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

@kennytm
Copy link
Member

kennytm commented Jul 18, 2018

The 'static bound of std::sync::Once has been removed in rust-lang/rust#52239 and would be available since 1.29.

@tbu-
Copy link
Contributor

tbu- commented Sep 10, 2018

This issue has been fixed in master (see above), and can be closed, I think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants