cocool97 / adb_client

Rust ADB (Android Debug Bridge) client library
95 stars 19 forks source link

ADB track_devices to take a callback thats closure friendly #12

Closed thulasi-ram closed 1 year ago

thulasi-ram commented 1 year ago

Currently the callback can only take fn pointers. I have an use case to pass the info onto a mpsc channel.

Let me know if there's another way to do it.

IIRC it should require a small change from

pub fn track_devices(&mut self, callback: fn(Device) -> Result<()>) -> Result<()> {

to

pub fn track_devices(&mut self, callback: impl Fn(Device) -> Result<()>) -> Result<()> {

thulasi-ram commented 1 year ago

My current use case below.. This gives compliation error: expected fn pointer, found closure

pub async fn adb_track_devices(c: &mut AdbTcpConnexion, tx: tokio::sync::MutexGuard<'_, Sender<String>>) -> Result<(), String> {

    let callback = |device:adb_client::Device| {
        let res = tx
            .send(format!("{}", device))
            .map_err(|e| e.to_string());

        return res;
    };

    let res = c.track_devices(callback);

    match res {
        Err(e) => {
            return Err(e.to_string());
        }
        Ok(_o) => {
            return Ok(());
        }
    }
}
cocool97 commented 1 year ago

Thanks for the report ! This is fixed in 0.6.0