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 slice::Vector for Option<T> and CVec<T> #16136

Closed

Conversation

donkopotamus
Copy link
Contributor

The std::slice::Vector trait effectively provides an interface for saying an object can give a slice view in to itself. Both Option<T> and CVec<T> expose as_slice but do so by implementing them directly rather than implementing them via the Vector trait.

This patch alters both Option<T> and CVec<T> to instead expose it by implementing Vector.

@lilyball
Copy link
Contributor

I'm not convinced it really makes sense to think of Option as a vector, but I guess it does already have the method.

@donkopotamus
Copy link
Contributor Author

I think, in truth, that the Vector trait is misnamed. Really it is a trait that says "you can get a readable array view into me".

If I can take it further ... currently it feels like there is an inconsistency in std::slice between the vector-like traits. There is:

  • Vector - an object can provide a slice view into itself (as_slice).
  • ImmutableVector - an object provides methods for slicing and dicing by integral index (and it doesn't inherit from Vector btw).
  • MutableVector - an object provides both a slice view into itself (as_mut_slice) and methods for directly manipulating that object.

In practice, this means I can write a generic function fn needs_slice<T: Vector>(x: &T) that manipulates x only via its as_slice representation. And I can easily declare that some new funky object can masquerade as a slice by simply implementing a single method. Good.

In contrast, I can also write a function fn needs_mut_slice<T: MutableVector>(x: &T) that manipulates x only via its slice representation as_mut_slice. But I can't declare that some new funky object can masquerade as a mutable slice without adding a bunch of other methods that I don't actually want exposed on that object ... when all I really want to do is implement just as_mut_slice. Less Good.

So it feels like there should be four traits:

  • SliceAccessor - an object can provide a slice view into itself (as_slice) (the existing Vector)
  • MutableSliceAccessor - an object can provide a mutable slice view into itself (as_mut_slice) (NEW)
  • ImmutableVector - an object provides a large number of methods for slicing and dicing itself.
  • MutableVector - an object provides methods for directly manipulating the object.

Sorry, all a bit long ...

@lilyball
Copy link
Contributor

ImmutableVector and MutableVector exist only because libcore cannot provide an impl on &[T] directly. This wasn't some grand plan for documenting functionality with traits.

@donkopotamus
Copy link
Contributor Author

@kballard Sure ... I just think that it might still prove useful if as_mut_slice was split out of MutableVector so it would be possible to write functions that would operate on anything that can provide a writable slice view in to themselves (happy to implement that)

Not very important!

@huonw
Copy link
Member

huonw commented Jul 31, 2014

FWIW, the Vector names are just hold-overs from history, they should all theoretically be called ...Slice... now.

@bluss
Copy link
Member

bluss commented Jul 31, 2014

So what do you think about implementing .as_slice() for all homogenous slices (T, ) and (T, T) and (T, T, T) .. etc?

@lilyball
Copy link
Contributor

@bluss AFAIK tuples don't guarantee that they are laid out in memory the same way an array would be, which would be necessary in order to turn them into slices.

@huonw
Copy link
Member

huonw commented Jul 31, 2014

So what do you think about implementing .as_slice() for all homogenous slices (T, ) and (T, T) and (T, T, T) .. etc?

The compiler actually already does this for it's own internal use.

lnicola pushed a commit to lnicola/rust that referenced this pull request Jan 3, 2024
…arter-with-fns, r=Veykril

fix(completion): make the expected type a tad smarter with `Fn`s

This commit changes how the expected type is calculated when
working with Fn pointers, making the parenthesis stop vanishing
when completing the function name.

I've been bugged by the behavior of parenthesis completion for
a long while now. R-a assumes that the `LetStmt` type is the same
as the function type I've just written. Worse is that all parenthesis
vanish, even from functions that have completely different signatures.
It will now verify if the signature is the same.

While working on this, I noticed that record fields behave the same,
so I also made it prioritize the field type instead of the current
expression when possible, but I'm unsure if this is OK, so input is
appreciated.

ImplTraits as return types will still behave weirdly because lowering
is disallowed at the time it resolves the function types.

![image](https://github.com/rust-lang/rust-analyzer/assets/29989290/c06d6c93-5cac-4ebe-a93b-923017a6ae8c)
![image](https://github.com/rust-lang/rust-analyzer/assets/29989290/31594d82-fa4d-446c-a77e-47e9de1a9a67)
![image](https://github.com/rust-lang/rust-analyzer/assets/29989290/cf33856e-a485-411b-91af-11090d78a44e)
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.

4 participants