Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Extend for Vec #64

Merged
merged 3 commits into from
May 22, 2024
Merged

Conversation

schneiderfelipe
Copy link
Contributor

@schneiderfelipe schneiderfelipe commented Feb 23, 2024

I would like to implement Extend<&'a #name> for #vec_name as well (just like std::vec::Vec<T>), but I would have to make sure #name is Copy (where #name: Copy does not help).

This is not the most performant implementation either, but it works.

Fix #51.

@schneiderfelipe schneiderfelipe changed the title Implement Extend for Vec (fix #51) Implement Extend for Vec Feb 23, 2024
@Luthaf
Copy link
Member

Luthaf commented Feb 24, 2024

I would like to implement Extend<&'a #name> for #vec_name

Maybe you can use the same kind of trick with higher-rank bounds as to_owned?

pub fn to_owned(&self) -> #name
// only expose to_owned is all fields are Clone
// https://github.com/rust-lang/rust/issues/48214#issuecomment-1150463333
where #( for<'b> #fields_types: Clone, )*

It would only check if all fields are copy and not the full struct, but this should be close enough.

@schneiderfelipe
Copy link
Contributor Author

I would like to implement Extend<&'a #name> for #vec_name

Maybe you can use the same kind of trick with higher-rank bounds as to_owned?

pub fn to_owned(&self) -> #name
// only expose to_owned is all fields are Clone
// https://github.com/rust-lang/rust/issues/48214#issuecomment-1150463333
where #( for<'b> #fields_types: Clone, )*

It would only check if all fields are copy and not the full struct, but this should be close enough.

This did the trick. Now we have both impl Extend<#name> for #vec_name and impl<'a> Extend<#ref_name<'a>> for #vec_name, the last one only available if all fields are Clone, so that to_owned can be called. This matches the available implementations on Vec<T>, impl<T, A> Extend<T> for Vec<T, A> and impl<'a, T, A> Extend<&'a T> for Vec<T, A>.

@Luthaf Luthaf merged commit bd5198a into lumol-org:master May 22, 2024
5 checks passed
@Luthaf
Copy link
Member

Luthaf commented May 22, 2024

This and #63 have been released on crates.io as soa-derive-internal==0.13.2

@schneiderfelipe schneiderfelipe deleted the impl-extend branch May 22, 2024 18:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Derive std::iter::Extend
2 participants