Merge pull request #137 from bluss/various

Combined fixes: Adjust inlining, and Fix usage of `get_unchecked_mut`.
This commit is contained in:
bluss
2019-10-09 10:47:21 +02:00
committed by GitHub
3 changed files with 15 additions and 27 deletions
-2
View File
@@ -87,10 +87,8 @@ 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]
fn as_slice(&self) -> &[Self::Item] { self } fn as_slice(&self) -> &[Self::Item] { self }
#[doc(hidden)] #[doc(hidden)]
#[inline]
fn as_mut_slice(&mut self) -> &mut [Self::Item] { self } fn as_mut_slice(&mut self) -> &mut [Self::Item] { self }
} }
) )
+1 -5
View File
@@ -113,7 +113,7 @@ impl<A> ArrayString<A>
/// let string = ArrayString::<[_; 3]>::new(); /// let string = ArrayString::<[_; 3]>::new();
/// assert_eq!(string.capacity(), 3); /// assert_eq!(string.capacity(), 3);
/// ``` /// ```
#[inline] #[inline(always)]
pub fn capacity(&self) -> usize { A::CAPACITY } pub fn capacity(&self) -> usize { A::CAPACITY }
/// Return if the `ArrayString` is completely filled. /// Return if the `ArrayString` is completely filled.
@@ -244,7 +244,6 @@ impl<A> ArrayString<A>
/// ///
/// assert_eq!(s.pop(), None); /// assert_eq!(s.pop(), None);
/// ``` /// ```
#[inline]
pub fn pop(&mut self) -> Option<char> { pub fn pop(&mut self) -> Option<char> {
let ch = match self.chars().rev().next() { let ch = match self.chars().rev().next() {
Some(ch) => ch, Some(ch) => ch,
@@ -273,7 +272,6 @@ impl<A> ArrayString<A>
/// string.truncate(4); /// string.truncate(4);
/// assert_eq!(&string[..], "foo"); /// assert_eq!(&string[..], "foo");
/// ``` /// ```
#[inline]
pub fn truncate(&mut self, new_len: usize) { pub fn truncate(&mut self, new_len: usize) {
if new_len <= self.len() { if new_len <= self.len() {
assert!(self.is_char_boundary(new_len)); assert!(self.is_char_boundary(new_len));
@@ -304,7 +302,6 @@ impl<A> ArrayString<A>
/// assert_eq!(s.remove(1), 'o'); /// assert_eq!(s.remove(1), 'o');
/// assert_eq!(s.remove(0), 'o'); /// assert_eq!(s.remove(0), 'o');
/// ``` /// ```
#[inline]
pub fn remove(&mut self, idx: usize) -> char { pub fn remove(&mut self, idx: usize) -> char {
let ch = match self[idx..].chars().next() { let ch = match self[idx..].chars().next() {
Some(ch) => ch, Some(ch) => ch,
@@ -336,7 +333,6 @@ impl<A> ArrayString<A>
/// ///
/// This method uses *debug assertions* to check the validity of `length` /// This method uses *debug assertions* to check the validity of `length`
/// and may use other debug assertions. /// and may use other debug assertions.
#[inline]
pub unsafe fn set_len(&mut self, length: usize) { pub unsafe fn set_len(&mut self, length: usize) {
debug_assert!(length <= self.capacity()); debug_assert!(length <= self.capacity());
self.len = Index::from(length); self.len = Index::from(length);
+14 -20
View File
@@ -134,7 +134,7 @@ impl<A: Array> ArrayVec<A> {
/// let array = ArrayVec::from([1, 2, 3]); /// let array = ArrayVec::from([1, 2, 3]);
/// assert_eq!(array.capacity(), 3); /// assert_eq!(array.capacity(), 3);
/// ``` /// ```
#[inline] #[inline(always)]
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.
@@ -235,14 +235,18 @@ impl<A: Array> ArrayVec<A> {
/// ///
/// assert_eq!(&array[..], &[1, 2]); /// assert_eq!(&array[..], &[1, 2]);
/// ``` /// ```
#[inline]
pub unsafe fn push_unchecked(&mut self, element: A::Item) { pub unsafe fn push_unchecked(&mut self, element: A::Item) {
let len = self.len(); let len = self.len();
debug_assert!(len < A::CAPACITY); debug_assert!(len < A::CAPACITY);
ptr::write(self.get_unchecked_mut(len), element); ptr::write(self.get_unchecked_ptr(len), element);
self.set_len(len + 1); self.set_len(len + 1);
} }
/// Get pointer to where element at `index` would be
unsafe fn get_unchecked_ptr(&mut self, index: usize) -> *mut A::Item {
self.xs.ptr_mut().add(index)
}
/// Insert `element` at position `index`. /// Insert `element` at position `index`.
/// ///
/// Shift up all elements after `index`. /// Shift up all elements after `index`.
@@ -300,7 +304,7 @@ impl<A: Array> ArrayVec<A> {
unsafe { // infallible unsafe { // infallible
// The spot to put the new value // The spot to put the new value
{ {
let p: *mut _ = self.get_unchecked_mut(index); let p: *mut _ = self.get_unchecked_ptr(index);
// Shift everything over to make space. (Duplicating the // Shift everything over to make space. (Duplicating the
// `index`th element into two consecutive places.) // `index`th element into two consecutive places.)
ptr::copy(p, p.offset(1), len - index); ptr::copy(p, p.offset(1), len - index);
@@ -334,7 +338,7 @@ impl<A: Array> ArrayVec<A> {
unsafe { unsafe {
let new_len = self.len() - 1; let new_len = self.len() - 1;
self.set_len(new_len); self.set_len(new_len);
Some(ptr::read(self.get_unchecked_mut(new_len))) Some(ptr::read(self.get_unchecked_ptr(new_len)))
} }
} }
@@ -507,7 +511,6 @@ impl<A: Array> ArrayVec<A> {
/// ///
/// This method uses *debug assertions* to check that `length` is /// This method uses *debug assertions* to check that `length` is
/// not greater than the capacity. /// not greater than the capacity.
#[inline]
pub unsafe fn set_len(&mut self, length: usize) { pub unsafe fn set_len(&mut self, length: usize) {
debug_assert!(length <= self.capacity()); debug_assert!(length <= self.capacity());
self.len = Index::from(length); self.len = Index::from(length);
@@ -628,7 +631,8 @@ impl<A: Array> ArrayVec<A> {
} }
} }
/// Dispose of `self` without the overwriting that is needed in Drop. /// Dispose of `self` (same as drop)
#[deprecated="Use std::mem::drop instead, if at all needed."]
pub fn dispose(mut self) { pub fn dispose(mut self) {
self.clear(); self.clear();
mem::forget(self); mem::forget(self);
@@ -754,7 +758,6 @@ pub struct IntoIter<A: Array> {
impl<A: Array> Iterator for IntoIter<A> { impl<A: Array> Iterator for IntoIter<A> {
type Item = A::Item; type Item = A::Item;
#[inline]
fn next(&mut self) -> Option<A::Item> { fn next(&mut self) -> Option<A::Item> {
if self.index == self.v.len { if self.index == self.v.len {
None None
@@ -762,7 +765,7 @@ impl<A: Array> Iterator for IntoIter<A> {
unsafe { unsafe {
let index = self.index.to_usize(); let index = self.index.to_usize();
self.index = Index::from(index + 1); self.index = Index::from(index + 1);
Some(ptr::read(self.v.get_unchecked_mut(index))) Some(ptr::read(self.v.get_unchecked_ptr(index)))
} }
} }
} }
@@ -774,7 +777,6 @@ impl<A: Array> Iterator for IntoIter<A> {
} }
impl<A: Array> DoubleEndedIterator for IntoIter<A> { impl<A: Array> DoubleEndedIterator for IntoIter<A> {
#[inline]
fn next_back(&mut self) -> Option<A::Item> { fn next_back(&mut self) -> Option<A::Item> {
if self.index == self.v.len { if self.index == self.v.len {
None None
@@ -782,7 +784,7 @@ impl<A: Array> DoubleEndedIterator for IntoIter<A> {
unsafe { unsafe {
let new_len = self.v.len() - 1; let new_len = self.v.len() - 1;
self.v.set_len(new_len); self.v.set_len(new_len);
Some(ptr::read(self.v.get_unchecked_mut(new_len))) Some(ptr::read(self.v.get_unchecked_ptr(new_len)))
} }
} }
} }
@@ -798,7 +800,7 @@ impl<A: Array> Drop for IntoIter<A> {
unsafe { unsafe {
self.v.set_len(0); self.v.set_len(0);
let elements = slice::from_raw_parts_mut( let elements = slice::from_raw_parts_mut(
self.v.get_unchecked_mut(index), self.v.get_unchecked_ptr(index),
len - index); len - index);
ptr::drop_in_place(elements); ptr::drop_in_place(elements);
} }
@@ -851,7 +853,6 @@ impl<'a, A: Array> Iterator for Drain<'a, A>
{ {
type Item = A::Item; type Item = A::Item;
#[inline]
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
self.iter.next().map(|elt| self.iter.next().map(|elt|
unsafe { unsafe {
@@ -860,7 +861,6 @@ impl<'a, A: Array> Iterator for Drain<'a, A>
) )
} }
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) { fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint() self.iter.size_hint()
} }
@@ -869,7 +869,6 @@ impl<'a, A: Array> Iterator for Drain<'a, A>
impl<'a, A: Array> DoubleEndedIterator for Drain<'a, A> impl<'a, A: Array> DoubleEndedIterator for Drain<'a, A>
where A::Item: 'a, where A::Item: 'a,
{ {
#[inline]
fn next_back(&mut self) -> Option<Self::Item> { fn next_back(&mut self) -> Option<Self::Item> {
self.iter.next_back().map(|elt| self.iter.next_back().map(|elt|
unsafe { unsafe {
@@ -1068,27 +1067,22 @@ impl<A: Array> Default for ArrayVec<A> {
} }
impl<A: Array> PartialOrd for ArrayVec<A> where A::Item: PartialOrd { impl<A: Array> PartialOrd for ArrayVec<A> where A::Item: PartialOrd {
#[inline]
fn partial_cmp(&self, other: &ArrayVec<A>) -> Option<cmp::Ordering> { fn partial_cmp(&self, other: &ArrayVec<A>) -> Option<cmp::Ordering> {
(**self).partial_cmp(other) (**self).partial_cmp(other)
} }
#[inline]
fn lt(&self, other: &Self) -> bool { fn lt(&self, other: &Self) -> bool {
(**self).lt(other) (**self).lt(other)
} }
#[inline]
fn le(&self, other: &Self) -> bool { fn le(&self, other: &Self) -> bool {
(**self).le(other) (**self).le(other)
} }
#[inline]
fn ge(&self, other: &Self) -> bool { fn ge(&self, other: &Self) -> bool {
(**self).ge(other) (**self).ge(other)
} }
#[inline]
fn gt(&self, other: &Self) -> bool { fn gt(&self, other: &Self) -> bool {
(**self).gt(other) (**self).gt(other)
} }