I want to get a discrimitant atomically

I'm thinking about writing some utilities, like a lock-free OnceCell.
Logically, it shouldn't need any extra space if the type has a niche.
is it possible to get a discriminant atomically, using Ordering::Acquire?

ps. I don't need the get_or_init operation.

note, the discriminant of an enum is an opaque type, casting a discriminant value to a numeric value is UB, unless the enum has only unit variants, or the enum is explicitly annotated with a #[repr(IntegerType)] attribute.

either way, it's not possible to access the discriminant value of an enum with niche-optimized representation.

I suggest you use a trait based design, although it would be rather cumbersome without the generic_atomic feature, which is still far from being stablized yet, you'll have to do a lot of boilerplate works (like marker traits) to make the API easier to use.

1 Like

Linking the docs for additional context.