sfackler / rust-postgres-array

MIT License
21 stars 10 forks source link

Add .extract() for extracting underlying Vec #7

Closed jonhoo closed 8 years ago

jonhoo commented 8 years ago

When fetching rows with rust-postgres, turning a Postgres Array from a returned row into a Rust Vec is not quite straightforward, and requirings going to an IntoIterator and back:

row.get::<_, Array<_>>(0).into_iter().collect()

This adds overhead, and could even add an allocation in the worst case. A better solution would be to provide a method for directly extracting the underlying Vec of a row.

This PR adds Array::extract(), which does just that. It consumes the Array, and returns the underlying Vec, allowing the code above to be expressed as:

row.get::<_, Array<_>>(0).extract()
sfackler commented 8 years ago

As a heads up, rust-postgres natively supports conversions of one dimensional postgres arrays to and from Rust slices and Vecs since version 0.11.5. This still seems reasonable to add for the multi dimensional case though.

into_inner is a more common name for this kind of conversion - could you renamve extract to that? Looks good other than that though, thanks!

jonhoo commented 8 years ago

Oh, neat! I can't find any mention of that in the rust-postgres readme beyond [u8] <-> BYTEA? into_inner is a better name, you're right.

sfackler commented 8 years ago

That's because I forgot to mention it in the table in the readme, will fix :). It is mentioned in the docs for FromSql and ToSql though: http://sfackler.github.io/rust-postgres/doc/v0.11.7/postgres/types/trait.FromSql.html#arrays.