Since Slices are always foreign types ToKind isn't implementable for &[MyStruct], even if MyStruct implements ToKinderror 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
Why?
Since Slices are always foreign types
ToKind
isn't implementable for&[MyStruct]
, even ifMyStruct
implementsToKind
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 invokingto_kind
, it would be nice to not have to do that.Solution
Implement
ToKind
for slices of typeT
.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 aToKind