Skip to content

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

@SoniEx2

Description

@SoniEx2

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions