• BB_C@programming.dev
    link
    fedilink
    arrow-up
    1
    ·
    4 hours ago

    Well I would say this is not at all specific to the codebase

    I was talking about what a codebase would want a non-Result-returning lock() to look like (the panic message, or forcing abort, extra logging, signal handling…etc).

    since poisoning is so rarely relevant.

    Poisoning (as a negative) is only relevant if we are pretending it matters for performance. And you already conceded that this is not a part of the conversation as far as you’re concerned.

    The “rarely relevant” (as in playing any operative role at all) is also pure conjecture, or if we want to be nicer, it’s totally contextual and use-case dependant.

    then using an external dependency is a terrible solution.

    lock_api provides a generic interface so you can e.g. seamlessly swipe out primitives from parking_lot with primitives from spin, you know, the whole point behind generic programming!

    This is in no way comparable with leftpad. And I wasn’t actually pointing to it as a solution (to the largely non-existing problem). I just wanted to point out that extension traits for synchronisation primitives already exist with wide usage in the ecosystem, since you seemed to not be aware.

    If you have an aversion to extension traits. Here is a trivial unit struct wrapper solution. You don’t even need to change the Mutex type name or the lock() method name in your code.

    use std::ops::{Deref, DerefMut};
    use std::sync::Mutex as PMutex;
    
    #[repr(transparent)]
    struct Mutex<T>(PMutex<T>);
    
    impl<T> Deref for Mutex<T> {
        type Target = PMutex<T>;
        fn deref(&self) -> &Self::Target { &self.0 }
    }
    
    impl<T> DerefMut for Mutex<T> {
        fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 }
    }
    
    impl<T> Mutex<T> {
        fn new(v: T) -> Self {
            Self(PMutex::new(v))
        }
    
        fn lock<'a>(&'a self) ->  std::sync::MutexGuard<'a, T> {
            self.0.lock().expect("poisoned")
        }
    }
    
    fn main() {
        let m = Mutex::new(false);
        // oh look, it's a non-Result returning lock()
        *m.lock() = true;
        dbg!(*m.lock());
        // inner Mutex method API is still available via Deref/DerefMut
        *m.try_lock().unwrap() = false;
        dbg!(*m.lock());
    }
    

    And yea you could say “duh just write the extension trait in your utils module” but this is just not acknowledging the problem at all and just sidestepping it. That’s not helping anyone.

    What is the notable relevant problem. Can you state it plainly?

    • SorteKanin@feddit.dkOP
      link
      fedilink
      arrow-up
      1
      ·
      3 hours ago

      I already told you, convenience, developer experience. Ergonomics basically. None of this is fixed by some code snippet, whether it’s an extension trait or wrapper type. That just makes things worse. This has nothing to do with performance, as I already said.

      • BB_C@programming.dev
        link
        fedilink
        arrow-up
        1
        ·
        2 hours ago

        convenience, developer experience. Ergonomics basically.

        Can you point the notable relevant inconvenience, bad developer experience, and poor ergonomics developers are suffering from?

        Is it all caused by a lock() method returning a Result?
        Anything else?