Amanieu / intrusive-rs

Intrusive collections for Rust
Apache License 2.0
423 stars 50 forks source link

Generic intrusive adaptors #1

Closed emberian closed 7 years ago

emberian commented 8 years ago

It would be nice to have the macro able to generate generic adaptors. For example, to make using the following nice:

pub struct Node<T> {
    link: linked_list::Link,
    val: T,
}

intrusive_adaptor!(for <T> List = Object<T> { free_list: linked_list::Link});

(Macro syntax come up with on-the-spot, not sure if that would parse)

Amanieu commented 8 years ago

I don't think this is possible with the current macro_rules! syntax. We could revisit this once rust-lang/rfcs#1583 lands, since it adds the ability to parse a generic argument list with macros.

For now you'll just have to manually implement the IntrusiveAdaptor trait.

emberian commented 8 years ago

It is possible in a limited way: you can't have bounds on the type parameters. This is still useful in the short term I think. (I'm working on a patch right now)

emberian commented 8 years ago

Well, I thought it was possible, seems not. Oh well.

Amanieu commented 7 years ago

Fixed in 0.5.0.

canarysnort01 commented 7 years ago

I am trying to make this work for a struct with two type parameters, like this:

pub struct ObjectSet<T, U> {
    set_list: linked_list::Link,
    pub objects: Vec<Object<T>>,
    pub metadata: U,
}

intrusive_adapter!(ObjectSetAdapter<T, U> = Box<ObjectSet<T, U>>: ObjectSet<T, U> { set_list: linked_list::Link });

However I'm getting this compiler error:

error: expected one of `,`, `:`, `=`, or `>`, found `T`
   |
53 | intrusive_adapter!(ObjectSetAdapter<T, U> = Box<ObjectSet<T,U>>: ObjectSet<T,U> { set_list: linked_list::Link });
   |                                     ^   - expected one of `,`, `:`, `=`, or `>` here
   |                                     |
   |                                     unexpected token

The compiler didn't complain about a similar intrusive_adapter! with one type argument.

Can you tell if I'm doing something wrong or if this might be a bug?

Amanieu commented 7 years ago

Fixed in 0.6.2.

canarysnort01 commented 7 years ago

Thanks :)