Skip to content
This repository was archived by the owner on Jul 30, 2025. It is now read-only.

Portable Implementation? / Calling lock in &self function? #11

@PTaylor-us

Description

@PTaylor-us

I'm relatively new to Rust, and I've been going in circles trying to figure this out. The problem I'm having is implementing a trait with a function taking &self in which I must lock a mutex_trait::Mutex implementation.

I am writing a library. I'm trying to make it generic for the implementation of mutex_trait::Mutex.

pub trait OtherTrait: Send + Sync {
    fn other_method(&self);
}


impl<M> Object<M>
    where
        &'static M: mutex_trait::Mutex<Data = MyData>,
{
    pub fn new(mtx: &'static M) -> Self {
        Self { mtx }
    }
}

impl<M> OtherTrait for Object<M>
    where
        &'static M: mutex_trait::Mutex<Data = MyData> + Send + Sync + 'static,
{
    fn other_method(&self) {
        self.mtx.lock(|data| do_thing(data));
    }
}

Playground

Using an implementation of mutex_trait::Mutex for &_ looked promising, but that would necessarily tie Object to the concrete type. I would prefer the mutex used to be shareable.

I don't know where to go from here and would greatly appreciate being pointed in the right direction.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions