Skip to content

Commit f2aad9f

Browse files
committed
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).
1 parent 6ed5b7b commit f2aad9f

File tree

3 files changed

+8
-23
lines changed

3 files changed

+8
-23
lines changed

src/array.rs

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,15 @@ pub unsafe trait Array {
2020
type Index: Index;
2121
/// The array's element capacity
2222
const CAPACITY: usize;
23-
#[doc(hidden)]
24-
fn as_ptr(&self) -> *const Self::Item;
25-
#[doc(hidden)]
26-
fn capacity() -> usize;
23+
fn as_slice(&self) -> &[Self::Item];
24+
fn as_mut_slice(&mut self) -> &mut [Self::Item];
2725
}
2826

2927
pub trait Index : PartialEq + Copy {
3028
fn to_usize(self) -> usize;
3129
fn from(usize) -> Self;
3230
}
3331

34-
use std::slice::{from_raw_parts};
35-
36-
pub trait ArrayExt : Array {
37-
#[inline(always)]
38-
fn as_slice(&self) -> &[Self::Item] {
39-
unsafe {
40-
from_raw_parts(self.as_ptr(), Self::capacity())
41-
}
42-
}
43-
}
44-
45-
impl<A> ArrayExt for A where A: Array { }
46-
4732
impl Index for () {
4833
#[inline(always)]
4934
fn to_usize(self) -> usize { 0 }
@@ -93,11 +78,11 @@ macro_rules! fix_array_impl {
9378
type Index = $index_type;
9479
const CAPACITY: usize = $len;
9580
#[doc(hidden)]
96-
#[inline(always)]
97-
fn as_ptr(&self) -> *const T { self as *const _ as *const _ }
81+
#[inline]
82+
fn as_slice(&self) -> &[Self::Item] { self }
9883
#[doc(hidden)]
99-
#[inline(always)]
100-
fn capacity() -> usize { $len }
84+
#[inline]
85+
fn as_mut_slice(&mut self) -> &mut [Self::Item] { self }
10186
}
10287
)
10388
}

src/array_string.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::str::FromStr;
99
use std::str::Utf8Error;
1010
use std::slice;
1111

12-
use array::{Array, ArrayExt};
12+
use array::Array;
1313
use array::Index;
1414
use CapacityError;
1515
use char::encode_utf8;

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ impl<A: Array> ArrayVec<A> {
155155
/// assert_eq!(array.capacity(), 3);
156156
/// ```
157157
#[inline]
158-
pub fn capacity(&self) -> usize { A::capacity() }
158+
pub fn capacity(&self) -> usize { A::CAPACITY }
159159

160160
/// Return if the `ArrayVec` is completely filled.
161161
///

0 commit comments

Comments
 (0)