3 Alright! We've got a decent minimal stack implemented. We can push, we can
4 pop, and we can clean up after ourselves. However there's a whole mess of
5 functionality we'd reasonably want. In particular, we have a proper array, but
6 none of the slice functionality. That's actually pretty easy to solve: we can
7 implement `Deref<Target=[T]>`. This will magically make our Vec coerce to, and
8 behave like, a slice in all sorts of conditions.
10 All we need is `slice::from_raw_parts`. It will correctly handle empty slices
11 for us. Later once we set up zero-sized type support it will also Just Work
17 impl<T> Deref for Vec<T> {
19 fn deref(&self) -> &[T] {
21 ::std::slice::from_raw_parts(self.ptr.as_ptr(), self.len)
27 And let's do DerefMut too:
30 use std::ops::DerefMut;
32 impl<T> DerefMut for Vec<T> {
33 fn deref_mut(&mut self) -> &mut [T] {
35 ::std::slice::from_raw_parts_mut(self.ptr.as_ptr(), self.len)
41 Now we have `len`, `first`, `last`, indexing, slicing, sorting, `iter`,
42 `iter_mut`, and all other sorts of bells and whistles provided by slice. Sweet!