Giter Site home page Giter Site logo

Comments (5)

kaoet avatar kaoet commented on August 25, 2024

Can we change the Resolver trait to use https://docs.rs/async_trait?

from domain.

glts avatar glts commented on August 25, 2024

I’m wondering if Resolver::Query should perhaps be Send? I am also running into a situation where it seems to me a Send future there would make the trait more generally usable?

For example, here is another program that would compile if Resolver::Query were Send:

use async_trait::async_trait;
use domain::resolv::StubResolver;
use std::net::IpAddr;

#[async_trait]
trait Lookup {
    async fn lookup_ptrs(&self, ip: IpAddr) -> Vec<String>;
}

struct Resolver {
    delegate: StubResolver,
}

#[async_trait]
impl Lookup for Resolver {
    async fn lookup_ptrs(&self, ip: IpAddr) -> Vec<String> {
        self.delegate
            .lookup_addr(ip)
            .await
            .unwrap()
            .into_iter()
            .map(|p| p.to_string())
            .collect()
    }
}

#[tokio::main]
async fn main() {
    let resolver = Resolver {
        delegate: StubResolver::new(),
    };

    let addrs = resolver.lookup_ptrs(IpAddr::from([8, 8, 8, 8])).await;

    println!("{:?}", addrs);
}

from domain.

partim avatar partim commented on August 25, 2024

Apologies for the ridiculously late answer!

Requiring Resolver::Query to be Send + 'static is a good idea, indeed. It should be easy to achieve this for stub::Query by placing the data of a StubResolver behind an arc and keep that in Query.

I’ll draft a PR for it and see if it really is all that simple.

from domain.

partim avatar partim commented on August 25, 2024

Can we change the Resolver trait to use https://docs.rs/async_trait?

The trait is already what I imagine async trait would turn it into. Resolver::query is basically an async function in anything but the name. It returns a future which you can await.

from domain.

partim avatar partim commented on August 25, 2024

Turns out that resolv::stub::Query is already Send, so requiring the trait in Resolver::Query is enough.

However, I now wonder if it might be worthwhile to turn this whole concept on its head by making the lookups implement a trait and ditch the Resolver trait. I think the lookups can be made in such a way that they don’t need to be aware of how the underlying querying mechanism works and whether it is synchronous or asynchronous atop Tokio or async-std or something else entirely.

from domain.

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.