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

as_ref and as_mut #62

Open
zesterer opened this issue Dec 18, 2020 · 2 comments
Open

as_ref and as_mut #62

zesterer opened this issue Dec 18, 2020 · 2 comments

Comments

@zesterer
Copy link
Contributor

zesterer commented Dec 18, 2020

I've found myself wanting methods similar to Option's as_ref and as_mut.

Their type signatures would be as follows:

impl<T> VecN<T> {
    fn as_ref(&self) -> VecN<&T>;
    fn as_mut(&mut self) -> VecN<&mut T>;
}

What do you think?

@yoanlcq
Copy link
Owner

yoanlcq commented Dec 18, 2020

I'm not against the idea, I just wonder if it could cause issues since vectors already implement AsRef<[T]> and AsMut<[T]>.

I also find myself using Option's as_ref quite often, but it doesn't implement AsRef so there is no ambiguity.
If we do choose these names (as_ref and as_mut), then we'd need to be very confident that these wouldn't cause breaking changes... Alternatively we could find other names, but that would be a pity since these ones are very good.

There's always the possibility to do v.map(|x| &x) but I get that it can be tiresome if repeated often.

One thing I'm also thinking about is how that well that would interact with repr_simd vectors; off the top of my head, either that is not supported, or that could cause various bad stuff (although it's the compiler's responsibility rather than ours, and one could argue that users simply shouldn't call as_ref() on SIMD vectors).

So I would say, "why not, let's try it out", and if it breaks something, well, we'll have to find another way.

@zesterer
Copy link
Contributor Author

There's always the possibility to do v.map(|x| &x) but I get that it can be tiresome if repeated often.

I don't believe that this is possible. VecN::map consumes self, yielding a vector containing references that point to something that no longer exists. This is at the heart of why I think this method (or something with a similar name) should be included: the only other way I've found to do this is to explicitly take a reference to the members like VecN::new(&my_vec.x, &my_vec.y, ...) which becomes very tedious with long vectors.

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

No branches or pull requests

2 participants