Iterate over vector of tuples of enum and arc


#1
pub trait MyTrait: Send + Sync {
    fn service_status(&self) -> ServiceStatus;
}

pub enum Type {
    ArcWithRwLock(Arc<RwLock<MyTrait>>),
    Arc(Arc<MyTrait>)
}

struct Catalog {
    pub services: Vec<Type>,
}

impl Catalog {

    pub fn health( &self ) -> Vec<ServiceStatus> {
        self.services.iter()
            .map(| service| {
                match *service {
                    ArcWithRwLock => service.read().unwrap().service_status,
                    _=> unimplemented!(),
                }
            })
            .collect()
    }

}

I have a trait that is implemented by multiple trait objects of different types declared in Type enum. I’m trying to iterate over a vec of trait objects and collect results from each objects based on type. But I get the following error-
error[E0599]: no method named read found for type &service::Type in the current scope

|

123 | ArcWithRwLock => service.read().unwrap().service_status,

though read() is a valid function.


#2

You need to qualify ArcWithRwLock with the enum type and also capture the inner value:

Type::ArcWithRwLock(ref s) => s.read().unwrap().service_status(),