API: Update ArrayExt's methods to expose slices instead of pointers

This is a nicer and simpler interface to expose, since the ref to uninit
array to pointer cast is not usable anyway (it's historic now, arrayvec
itself does not use it anymore).
This commit is contained in:
bluss
2019-07-10 18:13:06 +02:00
parent 6ed5b7b67f
commit f2aad9f9a7
3 changed files with 8 additions and 23 deletions
+6 -21
View File
@@ -20,10 +20,8 @@ pub unsafe trait Array {
type Index: Index; type Index: Index;
/// The array's element capacity /// The array's element capacity
const CAPACITY: usize; const CAPACITY: usize;
#[doc(hidden)] fn as_slice(&self) -> &[Self::Item];
fn as_ptr(&self) -> *const Self::Item; fn as_mut_slice(&mut self) -> &mut [Self::Item];
#[doc(hidden)]
fn capacity() -> usize;
} }
pub trait Index : PartialEq + Copy { pub trait Index : PartialEq + Copy {
@@ -31,19 +29,6 @@ pub trait Index : PartialEq + Copy {
fn from(usize) -> Self; fn from(usize) -> Self;
} }
use std::slice::{from_raw_parts};
pub trait ArrayExt : Array {
#[inline(always)]
fn as_slice(&self) -> &[Self::Item] {
unsafe {
from_raw_parts(self.as_ptr(), Self::capacity())
}
}
}
impl<A> ArrayExt for A where A: Array { }
impl Index for () { impl Index for () {
#[inline(always)] #[inline(always)]
fn to_usize(self) -> usize { 0 } fn to_usize(self) -> usize { 0 }
@@ -93,11 +78,11 @@ macro_rules! fix_array_impl {
type Index = $index_type; type Index = $index_type;
const CAPACITY: usize = $len; const CAPACITY: usize = $len;
#[doc(hidden)] #[doc(hidden)]
#[inline(always)] #[inline]
fn as_ptr(&self) -> *const T { self as *const _ as *const _ } fn as_slice(&self) -> &[Self::Item] { self }
#[doc(hidden)] #[doc(hidden)]
#[inline(always)] #[inline]
fn capacity() -> usize { $len } fn as_mut_slice(&mut self) -> &mut [Self::Item] { self }
} }
) )
} }
+1 -1
View File
@@ -9,7 +9,7 @@ use std::str::FromStr;
use std::str::Utf8Error; use std::str::Utf8Error;
use std::slice; use std::slice;
use array::{Array, ArrayExt}; use array::Array;
use array::Index; use array::Index;
use CapacityError; use CapacityError;
use char::encode_utf8; use char::encode_utf8;
+1 -1
View File
@@ -155,7 +155,7 @@ impl<A: Array> ArrayVec<A> {
/// assert_eq!(array.capacity(), 3); /// assert_eq!(array.capacity(), 3);
/// ``` /// ```
#[inline] #[inline]
pub fn capacity(&self) -> usize { A::capacity() } pub fn capacity(&self) -> usize { A::CAPACITY }
/// Return if the `ArrayVec` is completely filled. /// Return if the `ArrayVec` is completely filled.
/// ///