Closed Emilgardis closed 1 month ago
We could clarify in the documentation for the methods how to do this, or make new methods that take impl Iterator<Item = impl Into<&'a Borrowed>>
which are then collected and put in a Cow::Owned
.
I think I prefer the second alternative, since one would have to do an allocation either way if you have a Vec<Owned>
.
@laundmo also mentions in the discord thread a solution that would involve a impl CollectionRef
which abstracts over a collection, this would work for the impl Iterator
route, and it would also work for a route where we make the stored collection in the Request
generic over collection for those fields, albeit at a cost of more generics on that specific request.
heres my CollectionRef idea: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=14019aa8f991b7ec5eef82f29879a459
a great advantage is that this works on many collections and even individual items
This could also be applied for the client ext methods which currently takes impl AsRef<[&'client types::CategoryIdRef]>
also experimented with
fn into_slice_cow(self) -> std::borrow::Cow<'a, [R]>
where
[R]: ToOwned,
R: Sized + 'a, {
match self {
std::borrow::Cow::Borrowed(b) => {
std::borrow::Cow::Borrowed(std::slice::from_ref(b.into()))
}
std::borrow::Cow::Owned(o) => {
let a: std::borrow::Cow<'_, R> = std::borrow::Cow::Owned(o.into());
let a = std::slice::from_ref(a.as_ref());
std::borrow::Cow::Borrowed(a) // ~ cannot return value referencing local variable `a`
}
}
}
improved with #359
This came up in discussion over at Twitch API Discord
currently, it can be hard to reason how to deal with a method like
we have a bunch of these after #280
One way to deal with this, given we have a
Vec<String>
, isbut coming up with this is not apparent.
This is related to https://github.com/twitch-rs/twitch_api/issues/114#issuecomment-1323517268 where we would make the allocation optional