5

This is a pretty specific question related to the Rocket crate. I'm not sure if this is an issue with the crate or if I'm just doing something plain wrong. I'm not the best with lifetimes and macros.

I'm trying to setup an SSE connection, and I want the macro to borrow values that are passed into the function.

I initially followed their tutorial for setting up SSEs, and wrote the following code...

#[get("/my/stream")]
pub fn my_stream(config: &State<config::Config>, _pool: &State<Pool<Sqlite>>) -> EventStream![] {
    EventStream! {
        let mut id = 0;
        let mut interval = time::interval(Duration::from_secs(1));
        loop {
            yield Event::data("test data").event("test").id(id.to_string());
            interval.tick().await;
            id += 1;
        }
    } 
}

This compiles and works great, but when I try to do something simple and borrow either config or _pool then it complains about the lifetime in the macro. The compiler was pretty clear and it matched what they had in their documentation. So I added the lifetime to the return value.

#[get("/my/stream")]
pub fn my_stream(config: & State<config::Config>, _pool: &State<Pool<Sqlite>>) -> EventStream![Event + '_] {
    EventStream! {
        let _test = config.database.clone();
        let mut id = 0;
        let mut interval = time::interval(Duration::from_secs(1));
        loop {
            yield Event::data("test data").event("test").id(id.to_string());
            interval.tick().await;
            id += 1;
        }
    } 
}

However, once I do this it complains that Event is not a trait...? I looked through their source and I don't see Event as a trait. What am I doing wrong and why? The last error I receive is...

 pub fn my_stream(config: & State<config::Config>, _pool: &State<Pool<Sqlite>>) -> EventStream![Event + '_] {
   |                                                                                                ^^^^^ not a trait
   |
help: `+` is used to constrain a "trait object" type with lifetimes or auto-traits; structs and enums can't be bound in that way
  --> src/api/mod.rs:24:104
This is the output of cargo tree --depth 1
├── clap v2.27.1
├── rocket v0.5.0-rc.1
│   [build-dependencies]
├── serde v1.0.130
├── serde_json v1.0.66
├── sqlx v0.5.9
└── tokio v1.9.0
Taztingo
  • 1,915
  • 2
  • 27
  • 42
  • 1
    `Event + '_` when `Event` is not a trait is invalid syntax, but since its within a macro, its up to it whether the usage is valid or not. Though this definitely *looks* correct by the [documentation](https://api.rocket.rs/master/rocket/response/stream/struct.EventStream.html#borrowing). – kmdreko Oct 14 '21 at 21:33
  • just curious, does it work if you remove the `_pool` parameter? – kmdreko Oct 14 '21 at 21:34
  • @kmdreko - Removing pool doesn't make it work. – Taztingo Oct 15 '21 at 13:01
  • 1
    can you provide your `Cargo.toml` dependencies and/or the output of `cargo tree --depth 1` so we can see the exact versions you're using? – kmdreko Oct 17 '21 at 19:55
  • @kmdreko Sure, I attached the output of cargo tree -depth 1 :). – Taztingo Oct 18 '21 at 17:28
  • In addition to the bounty I placed, I have submitted an issue about this to the issue tracker of Rocket's main repo: https://github.com/SergioBenitez/Rocket/issues/2158 – Dante Falzone May 01 '22 at 04:36

0 Answers0