Re: [PATCH v7 6/9] rust: sync: atomic: Add the framework of arithmetic operations

From: Boqun Feng
Date: Wed Jul 16 2025 - 11:51:01 EST


On Wed, Jul 16, 2025 at 05:36:05PM +0200, Benno Lossin wrote:
[..]
> >
> > I have a better solution:
> >
> > in ops.rs
> >
> > pub struct AtomicRepr<T: AtomicImpl>(UnsafeCell<T>)
> >
> > impl AtomicArithmeticOps for i32 {
> > // a *safe* function
> > fn atomic_add(a: &AtomicRepr, v: i32) {
> > ...
> > }
> > }
> >
> > in generic.rs
> >
> > pub struct Atomic<T>(AtoimcRepr<T::Repr>);
> >
> > impl<T: AtomicAdd> Atomic<T> {
> > fn add(&self, v: .., ...) {
> > T::Repr::atomic_add(&self.0, ...);
> > }
> > }
> >
> > see:
> >
> > https://git.kernel.org/pub/scm/linux/kernel/git/boqun/linux.git/log/?h=rust-atomic-impl
>
> Hmm what does the additional indirection give you?
>

What additional indirection you mean? You cannot make atomic_add() safe
with only `UnsafeCell<T::Repr>`.

Regards,
Boqun

> Otherwise this looks like the `T::Repr` approach that I detailed above,
> so I like it :)
>
> ---
> Cheers,
> Benno