rethinkdb / rethinkdb-rs

A native RethinkDB driver written in Rust
Apache License 2.0
210 stars 27 forks source link

Some information on filter #18

Closed tavurth closed 6 years ago

tavurth commented 6 years ago

So I'm trying to write a simple filter to expand the changelog example so that it notes only changes which match our TestItem implementation, however I can't seem to get any filters working:

let query =
        r.db("test")
        .table("test")
        .filter(|doc| {
            doc.has_fields("test").and(doc.test.type_of("NUMBER"))
        })
        .changes()

Gives me:

error[E0619]: the type of this value must be known in this context
 |
 | .filter(|doc| { doc.has_fields("test").and(doc.test.type_of("NUMBER")) })
 |                 ^^^^^^^^^^^^^^^^^^^^^^

error[E0277]: the trait bound `[closure@src/main.rs:55:17: 55:81]: reql::IntoArg` is not satisfied
 |
 |.filter(|doc| { doc.has_fields("test").and(doc.test.type_of("NUMBER")) })
 | ^^^^^^ the trait `reql::IntoArg` is not implemented for `[closure@src/main.rs:55:17: 55:81]`

Any advice on the filter implementation would be appreciated, I'm rather new to Rust at this point.

rushmorem commented 6 years ago

ReQL closures can take multiple arguments. We can't reasonably implement all the possibilities using Rust's type system. Combined with the fact that ReQL has so many commands, I decided to generate the commands automatically straight from the ReQL documentation. To make this possible, I made the following design choices:-

The argument that the command takes must implement reql::IntoArg. As you can see from the second error message, we don't implement it for closures as this is not practical. Instead we use the args macro to pass in closures. So try this instead:-

let query =
        r.db("test")
        .table("test")
        .filter(args!(|doc| {
            doc.has_fields("test").and(doc.get_field("test").type_of().eq("NUMBER"))
        }))
        .changes()
tavurth commented 6 years ago

I see, that makes a lot more sense now, thank you!

I've updated the example code to reflect the filtered change.

rushmorem commented 6 years ago

I see, that makes a lot more sense now, thank you!

My pleasure :)

I've updated the example code to reflect the filtered change.

Awesome! Let me take a look. Thanks.