Code Monkey home page Code Monkey logo

Comments (3)

surban avatar surban commented on August 20, 2024

This would be a good extension to the API.

It would also make sense to provide a wrapper function around CharacteristicWriteMethod::Fun and similar types that perform the boxing of the specified closure, i.e. Box::new(move |...| { async move { ... }.boxed() }).

I'd start out with the inner ones (e.g.: Characteristic::builder) and progressively add the higher one.

Yes, we can then discuss the proposed builder pattern in more detail.

from bluer.

WhyNotHugo avatar WhyNotHugo commented on August 20, 2024

It would also make sense to provide a wrapper function around CharacteristicWriteMethod::Fun and similar types that perform the boxing of the specified closure, i.e. Box::new(move |...| { async move { ... }.boxed() }).

Yeah, that's also kind ugly and repetitive. Ideally, the following:

CharacteristicWriteMethod::Fun(Box::new(move |new_value, req| {
    async move {
        todo!("{:?} {:?}", new_value, req);
    }
    .boxed()
})),

Could be written as:

CharacteristicWriteMethod::from(async move {
    todo!("{:?} {:?}", new_value, req);
}),

However, async closures are unstable: rust-lang/rust#62290.

I used bluer/examples/gatt_server_cb.rs for some experimenting around simplifying this bit anyway. I ended up hitting lifetime issues that come up due to the async block moving variables into its scope.

I'm not sure that there's anything actionable on simplifying CharacteristicWriteMethod definition.

from bluer.

surban avatar surban commented on August 20, 2024

You don't need async closures for that. You can create a wrapper function like this:

use futures::future::FutureExt;
use std::future::Future;
use std::io::Result;
use std::pin::Pin; // 0.3.30

pub type CharacteristicWriteFun =
    Box<dyn Fn(Vec<u8>, String) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> + Send + Sync>;

pub enum CharacteristicWriteMethod {
    Fun(CharacteristicWriteFun),
    Io,
}

impl CharacteristicWriteMethod {
    pub fn fun<Fut>(f: impl Fn(Vec<u8>, String) -> Fut + Send + Sync + 'static) -> Self
    where
        Fut: Future<Output = Result<()>> + Send + 'static,
    {
        Self::Fun(Box::new(move |a, b| f(a, b).boxed()))
    }
}

fn main() {
    CharacteristicWriteMethod::fun(|a, b| async move { Ok(()) });
}

See https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=e1d0f1b90e94971cade64cc536713070

from bluer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.