Re: [PATCH v1 1/2] rust: Add support for calling a function exactly once

FUJITA Tomonori
2025-11-11T23:38:15Z

On Tue, 11 Nov 2025 10:12:36 +0100
Andreas Hindborg <a.hindborg@kernel.org> wrote:

>>> error[E0277]: `core::cell::UnsafeCell<core::mem::MaybeUninit<()>>` cannot be shared between threads safely
>>> --> /home/fujita/git/linux-rust/samples/rust/rust_configfs.rs:109:9
>>> |
>>> 109 | kernel::pr_info_once!("print once\n");
>>> | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `core::cell::UnsafeCell<core::mem::MaybeUninit<()>>` cannot be shared between threads safely
>>>
>>>
>>> We need Sync for SetOnce? I haven't been following the SetOnce
>>
>> Yes, I think `SetOnce<T>` should be `Sync` if T is `Sync`. So good
>> finding!
>
> @Tomonori you can include a patch to add the impl, if you end up using
> `SetOnce`.

I was planning to send a patch for adding Send and Sync anyway,
regardless of whether I end up using SetOnce for OnceLite.

Another change I'd like to propose is to use an enum, such as State,
instead of u32 for the init member, as I did in the OnceLite
implementation [1], something like:

+#[derive(Clone, Copy, PartialEq, Eq)]
+#[repr(i32)]
+enum State {
+ NotStarted = 0,
+ InProgress = 1,
+ Completed = 2,
+}
+
+unsafe impl AtomicType for State {
+ type Repr = i32;
+}
+

I think that this would make the code's intent clearer. What do you
think?

If you think that change sounds reasonable, I can send a patch.

[1] https://lore.kernel.org/rust-for-linux/20251105054731.3194118-2-fujita.tomonori@gmail.com/