GithubHelp home page GithubHelp logo

Consider an `IntoRequest` trait about tonic HOT 4 CLOSED

hyperium avatar hyperium commented on July 30, 2024 6
Consider an `IntoRequest` trait

from tonic.

Comments (4)

carllerche avatar carllerche commented on July 30, 2024 1

For this to work out, there would need to be two traits. IntoRequest and IntoStreamingRequest. Otherwise, you get into coherence problems.

Sketch:

struct Request<T>(T);

trait Message: std::fmt::Display {}

impl Message for &'static str {}

trait IntoRequest {
    type Message: Message;
    
    fn into_request(self) -> Request<Self::Message>;
}

trait IntoStreamingRequest {
    type Stream: Iterator<Item = Self::Message>;
    type Message: Message;
    
    fn into_streaming_request(self) -> Request<Self::Stream>;
}

impl<T: Message> IntoRequest for Request<T> {
    type Message = T;
    
    fn into_request(self) -> Self {
        self
    }
}

impl<T: Iterator> IntoStreamingRequest for Request<T> where T::Item: Message {
    type Stream = T;
    type Message = T::Item;
    
    fn into_streaming_request(self) -> Self {
        self
    }
}

impl<T: Iterator> IntoStreamingRequest for T where T::Item: Message {
    type Stream = T;
    type Message = T::Item;
    
    fn into_streaming_request(self) -> Request<Self> {
        Request(self)
    }
}

fn say_hello<T: IntoStreamingRequest<Message = &'static str>>(t: T) {
    println!("== say hello");
    for msg in t.into_streaming_request().0 {
        println!(" + {}", msg);
    }
}

fn main() {
    say_hello(vec!["hello", "world"].into_iter());
    say_hello(Request(vec!["hello", "world"].into_iter()));
}

from tonic.

ChetanBhasin avatar ChetanBhasin commented on July 30, 2024

Hey! I'm fairly new to this project, but the library seems fantastic so far!

This looks like a nice first issue. I was hoping to take a crack at it sometime this week or over the weekend if a PR is welcome. 😃

from tonic.

hawkw avatar hawkw commented on July 30, 2024

@ChetanBhasin I'm not sure if this is actually going to be easy, or even possible — I took a crack at it earlier, and ran into some issues with type inference. If you want to take a look at it, you may have more success than I did, but I just wanted to warn you.

from tonic.

hawkw avatar hawkw commented on July 30, 2024

Oh, the solution of adding a separate trait for streaming requests is obvious in hindsight, nice!

from tonic.

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.