al8n / stretto

Stretto is a Rust implementation for Dgraph's ristretto (https://github.com/dgraph-io/ristretto). A high performance memory-bound Rust cache.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Async use doesn't appear to work?

rhomber opened this issue · comments

error[E0308]: mismatched types
  --> lit-attestation-service/src/handlers/attestation_intent.rs:34:9
   |
34 | /         Box::pin(async move {
35 | |             debug!(req = as_serde!(req); "AttestationIntentHandler");
36 | |
37 | |             // Create initial Attestation object.
...  |
77 | |             Ok(AttestationIntentResp { attestation, session_id })
78 | |         })
   | |__________^ one type is more general than the other
   |
   = note: expected struct `Box<dyn Any + std::marker::Send + Sync>`
              found struct `Box<dyn Any + std::marker::Send + Sync>`

error: higher-ranked lifetime error
  --> lit-attestation-service/src/handlers/attestation_intent.rs:34:9
   |
34 | /         Box::pin(async move {
35 | |             debug!(req = as_serde!(req); "AttestationIntentHandler");
36 | |
37 | |             // Create initial Attestation object.
...  |
77 | |             Ok(AttestationIntentResp { attestation, session_id })
78 | |         })
   | |__________^
   |
   = note: could not prove `Pin<Box<[async block@lit-attestation-service/src/handlers/attestation_intent.rs:34:18: 78:10]>>: CoerceUnsized<Pin<Box<(dyn futures::Future<Output = std::result::Result<AttestationIntentResp, lit_attestation::Error>> + std::marker::Send + 'b)>>>`

Which happens when i use async_trait or directly use BoxFuture. It all relates to this line:

CACHE.insert(session_id.clone(), Box::new(attestation.clone()), 1).await;

My cache is constructed:

pub static CACHE: Lazy<AsyncCache<String, Box<dyn Any + Send + Sync>>> =
    Lazy::new(|| AsyncCache::new(100, 10, tokio::spawn).expect("failed to create cache"));

Someone in the rust community (bruh![moment]) was able to get it working, if we create:

fn cache_session(session_id: String, attestation: Attestation) -> BoxFuture<'static, bool> {
    CACHE.insert(session_id, Box::new(attestation), 1).boxed()
}

and call that instead, it works!

So perhaps you need to provide a BoxFuture instead here?

commented

Thanks! I will look at it later.