Skip to content

Commit b7b8cf8

Browse files
authored
Merge pull request #137 from bluss/various
Combined fixes: Adjust inlining, and Fix usage of `get_unchecked_mut`.
2 parents 2a376d9 + fd72321 commit b7b8cf8

File tree

3 files changed

+15
-27
lines changed

3 files changed

+15
-27
lines changed

src/array.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,8 @@ macro_rules! fix_array_impl {
8787
type Index = $index_type;
8888
const CAPACITY: usize = $len;
8989
#[doc(hidden)]
90-
#[inline]
9190
fn as_slice(&self) -> &[Self::Item] { self }
9291
#[doc(hidden)]
93-
#[inline]
9492
fn as_mut_slice(&mut self) -> &mut [Self::Item] { self }
9593
}
9694
)

src/array_string.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ impl<A> ArrayString<A>
113113
/// let string = ArrayString::<[_; 3]>::new();
114114
/// assert_eq!(string.capacity(), 3);
115115
/// ```
116-
#[inline]
116+
#[inline(always)]
117117
pub fn capacity(&self) -> usize { A::CAPACITY }
118118

119119
/// Return if the `ArrayString` is completely filled.
@@ -244,7 +244,6 @@ impl<A> ArrayString<A>
244244
///
245245
/// assert_eq!(s.pop(), None);
246246
/// ```
247-
#[inline]
248247
pub fn pop(&mut self) -> Option<char> {
249248
let ch = match self.chars().rev().next() {
250249
Some(ch) => ch,
@@ -273,7 +272,6 @@ impl<A> ArrayString<A>
273272
/// string.truncate(4);
274273
/// assert_eq!(&string[..], "foo");
275274
/// ```
276-
#[inline]
277275
pub fn truncate(&mut self, new_len: usize) {
278276
if new_len <= self.len() {
279277
assert!(self.is_char_boundary(new_len));
@@ -304,7 +302,6 @@ impl<A> ArrayString<A>
304302
/// assert_eq!(s.remove(1), 'o');
305303
/// assert_eq!(s.remove(0), 'o');
306304
/// ```
307-
#[inline]
308305
pub fn remove(&mut self, idx: usize) -> char {
309306
let ch = match self[idx..].chars().next() {
310307
Some(ch) => ch,
@@ -336,7 +333,6 @@ impl<A> ArrayString<A>
336333
///
337334
/// This method uses *debug assertions* to check the validity of `length`
338335
/// and may use other debug assertions.
339-
#[inline]
340336
pub unsafe fn set_len(&mut self, length: usize) {
341337
debug_assert!(length <= self.capacity());
342338
self.len = Index::from(length);

src/lib.rs

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ impl<A: Array> ArrayVec<A> {
134134
/// let array = ArrayVec::from([1, 2, 3]);
135135
/// assert_eq!(array.capacity(), 3);
136136
/// ```
137-
#[inline]
137+
#[inline(always)]
138138
pub fn capacity(&self) -> usize { A::CAPACITY }
139139

140140
/// Return if the `ArrayVec` is completely filled.
@@ -235,14 +235,18 @@ impl<A: Array> ArrayVec<A> {
235235
///
236236
/// assert_eq!(&array[..], &[1, 2]);
237237
/// ```
238-
#[inline]
239238
pub unsafe fn push_unchecked(&mut self, element: A::Item) {
240239
let len = self.len();
241240
debug_assert!(len < A::CAPACITY);
242-
ptr::write(self.get_unchecked_mut(len), element);
241+
ptr::write(self.get_unchecked_ptr(len), element);
243242
self.set_len(len + 1);
244243
}
245244

245+
/// Get pointer to where element at `index` would be
246+
unsafe fn get_unchecked_ptr(&mut self, index: usize) -> *mut A::Item {
247+
self.xs.ptr_mut().add(index)
248+
}
249+
246250
/// Insert `element` at position `index`.
247251
///
248252
/// Shift up all elements after `index`.
@@ -300,7 +304,7 @@ impl<A: Array> ArrayVec<A> {
300304
unsafe { // infallible
301305
// The spot to put the new value
302306
{
303-
let p: *mut _ = self.get_unchecked_mut(index);
307+
let p: *mut _ = self.get_unchecked_ptr(index);
304308
// Shift everything over to make space. (Duplicating the
305309
// `index`th element into two consecutive places.)
306310
ptr::copy(p, p.offset(1), len - index);
@@ -334,7 +338,7 @@ impl<A: Array> ArrayVec<A> {
334338
unsafe {
335339
let new_len = self.len() - 1;
336340
self.set_len(new_len);
337-
Some(ptr::read(self.get_unchecked_mut(new_len)))
341+
Some(ptr::read(self.get_unchecked_ptr(new_len)))
338342
}
339343
}
340344

@@ -507,7 +511,6 @@ impl<A: Array> ArrayVec<A> {
507511
///
508512
/// This method uses *debug assertions* to check that `length` is
509513
/// not greater than the capacity.
510-
#[inline]
511514
pub unsafe fn set_len(&mut self, length: usize) {
512515
debug_assert!(length <= self.capacity());
513516
self.len = Index::from(length);
@@ -628,7 +631,8 @@ impl<A: Array> ArrayVec<A> {
628631
}
629632
}
630633

631-
/// Dispose of `self` without the overwriting that is needed in Drop.
634+
/// Dispose of `self` (same as drop)
635+
#[deprecated="Use std::mem::drop instead, if at all needed."]
632636
pub fn dispose(mut self) {
633637
self.clear();
634638
mem::forget(self);
@@ -754,15 +758,14 @@ pub struct IntoIter<A: Array> {
754758
impl<A: Array> Iterator for IntoIter<A> {
755759
type Item = A::Item;
756760

757-
#[inline]
758761
fn next(&mut self) -> Option<A::Item> {
759762
if self.index == self.v.len {
760763
None
761764
} else {
762765
unsafe {
763766
let index = self.index.to_usize();
764767
self.index = Index::from(index + 1);
765-
Some(ptr::read(self.v.get_unchecked_mut(index)))
768+
Some(ptr::read(self.v.get_unchecked_ptr(index)))
766769
}
767770
}
768771
}
@@ -774,15 +777,14 @@ impl<A: Array> Iterator for IntoIter<A> {
774777
}
775778

776779
impl<A: Array> DoubleEndedIterator for IntoIter<A> {
777-
#[inline]
778780
fn next_back(&mut self) -> Option<A::Item> {
779781
if self.index == self.v.len {
780782
None
781783
} else {
782784
unsafe {
783785
let new_len = self.v.len() - 1;
784786
self.v.set_len(new_len);
785-
Some(ptr::read(self.v.get_unchecked_mut(new_len)))
787+
Some(ptr::read(self.v.get_unchecked_ptr(new_len)))
786788
}
787789
}
788790
}
@@ -798,7 +800,7 @@ impl<A: Array> Drop for IntoIter<A> {
798800
unsafe {
799801
self.v.set_len(0);
800802
let elements = slice::from_raw_parts_mut(
801-
self.v.get_unchecked_mut(index),
803+
self.v.get_unchecked_ptr(index),
802804
len - index);
803805
ptr::drop_in_place(elements);
804806
}
@@ -851,7 +853,6 @@ impl<'a, A: Array> Iterator for Drain<'a, A>
851853
{
852854
type Item = A::Item;
853855

854-
#[inline]
855856
fn next(&mut self) -> Option<Self::Item> {
856857
self.iter.next().map(|elt|
857858
unsafe {
@@ -860,7 +861,6 @@ impl<'a, A: Array> Iterator for Drain<'a, A>
860861
)
861862
}
862863

863-
#[inline]
864864
fn size_hint(&self) -> (usize, Option<usize>) {
865865
self.iter.size_hint()
866866
}
@@ -869,7 +869,6 @@ impl<'a, A: Array> Iterator for Drain<'a, A>
869869
impl<'a, A: Array> DoubleEndedIterator for Drain<'a, A>
870870
where A::Item: 'a,
871871
{
872-
#[inline]
873872
fn next_back(&mut self) -> Option<Self::Item> {
874873
self.iter.next_back().map(|elt|
875874
unsafe {
@@ -1068,27 +1067,22 @@ impl<A: Array> Default for ArrayVec<A> {
10681067
}
10691068

10701069
impl<A: Array> PartialOrd for ArrayVec<A> where A::Item: PartialOrd {
1071-
#[inline]
10721070
fn partial_cmp(&self, other: &ArrayVec<A>) -> Option<cmp::Ordering> {
10731071
(**self).partial_cmp(other)
10741072
}
10751073

1076-
#[inline]
10771074
fn lt(&self, other: &Self) -> bool {
10781075
(**self).lt(other)
10791076
}
10801077

1081-
#[inline]
10821078
fn le(&self, other: &Self) -> bool {
10831079
(**self).le(other)
10841080
}
10851081

1086-
#[inline]
10871082
fn ge(&self, other: &Self) -> bool {
10881083
(**self).ge(other)
10891084
}
10901085

1091-
#[inline]
10921086
fn gt(&self, other: &Self) -> bool {
10931087
(**self).gt(other)
10941088
}

0 commit comments

Comments
 (0)