hobofan / ambassador

Delegation of trait implementations via procedural macros
Apache License 2.0
251 stars 13 forks source link

add support for expr #20

Closed clouds56 closed 2 years ago

clouds56 commented 4 years ago

make things like work

#[derive(Delegate)]
#[delegate(MyTrait, target = "self.0.lock().unwrap()")]
pub struct Wrapper(Arc<Mutex<State>>);

or consider backward compatibility

#[derive(Delegate)]
#[delegate(MyTrait, method = "inner")]
pub struct Wrapper(Arc<Mutex<State>>);
impl Wrapper {
  fn inner(&self) -> MutexGuard<'_, State> {
    self.0.lock().wrapper()
  }
}
cameronbraid commented 1 year ago

I was hoping that https://github.com/hobofan/ambassador/pull/33 would allow me to do delegation like the following (similar to above example)

#[derive(Debug)]
pub struct RwLockMockProductDaoImpl(tokio::sync::RwLock<MockProductDaoImpl>);

#[async_trait]
impl ProductDaoDelegate for RwLockMockProductDaoImpl {
    async fn get_for_id(&self, id: i64) -> Result<Option<ProductModel>> {
        let guard = self.0.read().await;
        guard.get_for_id(id).await
    }
}

However I am unable to work it out.

Is someone able to given an example of delegating to a new type that wraps a tokio::sync::RwLock