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

impl Serialize for OnceCell #1952

Open
tamuhey opened this issue Jan 18, 2021 · 2 comments
Open

impl Serialize for OnceCell #1952

tamuhey opened this issue Jan 18, 2021 · 2 comments

Comments

@tamuhey
Copy link

tamuhey commented Jan 18, 2021

Like RefCell, it is useful if OnceCell can be serialized with serde.

Techcable added a commit to Techcable/serde that referenced this issue Oct 17, 2021
See also serde-rs#812 for other unstable functionality in serde.

Fixes serde-rs#1952
@Nohac
Copy link

Nohac commented Oct 21, 2024

I see the PR was closed due to lack of real-world use cases, so I wanted share how I would use it.

I'm currently working on a DSL where I first produce an AST, I then do some validation, as well as adding type information to each node in the AST. Adding the type information only happens once, but it requires that the full AST exists first.

To me, OnceCell seems to be the perfect candidate for this type of lazy/late initialization, as it doesn't require mutable access, and returns Option<&T> instead of Ref<'_, T> (which I find to be very annoying to work with).
It's the perfect alternative to RefCell<Option<T>> (or other mutable types) where the value only needs to be initiated once, it is also the least verbose.

The reason I need the AST to be serializable is so I can use it for code generation from different languages like typescript etc. later on.

I understand that this probably won't make you reconsider. I'm also evaluating alternatives to the way I'm currently doing things, but having support for OnceCell would make things a bit simpler.

Edit: The same goes for OnceLock

@jayvdb
Copy link

jayvdb commented Nov 22, 2024

fwiw, I am haphazardly maintaining https://crates.io/crates/once_cell_serde

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

Successfully merging a pull request may close this issue.

3 participants