Skip to content

Example for on_event #48

@flying-sheep

Description

@flying-sheep

Hi! I’m a Rust noob so forgive me:

If I follow the advice in the example and try to convert to on_event, I end up with something like:

central.start_scan().unwrap();
central.on_event(Box::new(|e: CentralEvent| match e {
    CentralEvent::DeviceDiscovered(addr) => {
        central.peripheral(addr).map(|lamp| {
            lamp.connect().unwrap();
            ...
        });
    },
    _ => {},
}));
// end of main()

Which results in

error[E0597]: `central` does not live long enough
  --> src/main.rs:38:13
   |
36 |       central.on_event(Box::new(|e: CentralEvent| match e {
   |                        -        ----------------- value captured here
   |  ______________________|
   | |
37 | |         CentralEvent::DeviceDiscovered(addr) => {
38 | |             central.peripheral(addr).map(|lamp| {
   | |             ^^^^^^^ borrowed value does not live long enough
39 | |                 lamp.connect().unwrap();
...  |
55 | |         _ => {},
56 | |     }));
   | |______- cast requires that `central` is borrowed for `'static`
57 |   }
   |   - `central` dropped here while still borrowed

How do I correctly box this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions