Re: [PATCH v4 03/10] rust: sync: atomic: Add ordering annotation types

Boqun Feng <[email protected]>
Newsgroups dev.linux.lists.lkmm,org.kernel.vger.linux-arch,org.kernel.vger.linux-kernel,org.kernel.vger.rust-for-linux
Message-ID <[email protected]>
On Tue, Jun 10, 2025 at 10:30:55AM -0700, Boqun Feng wrote:
[...]
> > > +/// Describes the exact memory ordering of an `impl` [`All`].
> > > +pub enum OrderingDesc {
> > 
> > Why not name this `Ordering`?
> > 
> 
> I was trying to avoid having an `Ordering` enum in a `ordering` mod.
> Also I want to save the name "Ordering" for the generic type parameter
> of an atomic operation, e.g.
> 
>     pub fn xchg<Ordering: ALL>(..)
> 
> this enum is more of an internal implementation detail, and users should
> not use this enum directly, so I would like to avoid potential
> confusion.
> 
> I have played a few sealed trait tricks on my end, but seems I cannot
> achieve:
> 
> 1) `OrderingDesc` is only accessible in the atomic mod.
> 2) `All` is only impl-able in the atomic mod, while it can be used as a
> trait bound outside kernel crate.
> 
> Maybe there is a trick I'm missing?
> 

Something like this seems to work:

    pub(super) mod private {
        /// Describes the exact memory ordering of an `impl` [`All`].
        pub enum Ordering {
            /// Relaxed ordering.
            Relaxed,
            /// Acquire ordering.
            Acquire,
            /// Release ordering.
            Release,
            /// Fully-ordered.
            Full,
        }
    
        pub trait HasOrderingDesc {
            /// Describes the exact memory ordering.
            const ORDERING: Ordering;
        }
    }

    /// The trait bound for annotating operations that should support all orderings.
    pub trait All: private::HasOrderingDesc { }

    impl private::HasOrderingDesc for Relaxed {
        const ORDERING: private::Ordering = private::Ordering::Relaxed;
    }

the trick is to seal the enum and the trait together.

Regards,
Boqun

> > > +    /// Relaxed ordering.
> > > +    Relaxed,
> > > +    /// Acquire ordering.
> > > +    Acquire,
> > > +    /// Release ordering.
> > > +    Release,
> > > +    /// Fully-ordered.
> > > +    Full,
> > > +}
> > > +
> > > +/// The trait bound for annotating operations that should support all orderings.
> > > +pub trait All {
> > > +    /// Describes the exact memory ordering.
> > > +    const ORDER: OrderingDesc;
> > 
> > And then here: `ORDERING`.
> 
[..]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.