yoshidan / google-cloud-rust

Google Cloud Client Libraries for Rust.
MIT License
217 stars 81 forks source link

Impl ToKind for slices #270

Closed MarcusGrass closed 1 month ago

MarcusGrass commented 1 month ago

Why?

Since Slices are always foreign types ToKind isn't implementable for &[MyStruct], even if MyStruct implements ToKind error reference.

An implementation exists for Vec<T>, but that would require a triple-allocation in some cases, first the upstream to create the collection, then the api which gets a slice, then the downstream implementation (this one) which creates a new vector by invoking to_kind, it would be nice to not have to do that.

Solution

Implement ToKind for slices of type T.

Just copies the vec implementation which doesn't need ownership of the vector since it invokes to_kind from references. The implementation then inlines a proxy call to the slice implementation in the vec implementation, because they're identical.

Notes

The Vec impl isn't really necessary since it doesn't require ownership and could be removed in a later major version, then a user could just do a my_vec.as_ref().to_kind(), or pass &my_vec into any function that takes a ToKind

yoshidan commented 1 month ago

Thanks!