anterofit icon indicating copy to clipboard operation
anterofit copied to clipboard

generic service traits

Open little-dude opened this issue 8 years ago • 2 comments

Hi,

I'd like to be able to write something like this:

service! {
    pub trait MyService[T] {
        // [...]
    }
 
   impl for Foo {
        // [...]
    }
    
    impl[U] for U [where U: AsRef<Foo>] {
        // [...]
    }
}

I tried to tweak the service! macro. See https://github.com/little-dude/anterofit/commit/4fe785a58f71f9dc1db7f654fd8e92302b4fc06a (sorry if I did something horrible here, this is my first time dealing with macros).

I tried it on a simple example (also avalaible on github in case you want to try it):

pub struct Foo<T> {
    phantom: PhantomData<T>,
    adapter: JsonAdapter,
}


service! {
    pub trait MyService[T] {
        fn get_next(&self) -> TryWithRaw<Foo<T>> {
            GET("")
        }
    }

    impl for Foo<T> {
        |this| &this.adapter
    }

    impl[U] for U [where U: AsRef<Foo<T>>] {
        |this| &this.as_ref().adapter
    }
}

The generated code look ok, but I'm hitting that error which I'm not sure how to solve:

error[E0277]: the trait bound `anterofit::net::response::TryWithRaw<Foo<T>>: anterofit::net::FromResponse` is not satisfied
  --> src/lib.rs:14:1
   |
14 | service! {
   | ^ the trait `anterofit::net::FromResponse` is not implemented for `anterofit::net::response::TryWithRaw<Foo<T>>`
   |
   = help: the following implementations were found:
             <anterofit::net::response::TryWithRaw<T> as anterofit::net::FromResponse>
   = note: this error originates in a macro outside of the current crate

I don't really understand because FromResponse is implemented for TryWithRaw<T>, so it should also work for TryWithRaw<Foo<T>> no?

little-dude avatar Mar 26 '17 18:03 little-dude

I've been working in a better solution for generics on the parse-generics branch, it's coming along okay so far. It shouldn't be too hard to extend it to generics on traits.

abonander avatar Mar 26 '17 18:03 abonander

You're awesome, I'll take a look 👍

little-dude avatar Mar 26 '17 19:03 little-dude