22: elide bounds check in Vec.deref{,_mut} r=japaric a=japaric



Co-authored-by: Jorge Aparicio <jorge@japaric.io>
This commit is contained in:
bors[bot] 2018-04-13 14:26:38 +00:00
commit 3f8c99e1e9
7 changed files with 39 additions and 54 deletions

View File

@ -33,10 +33,7 @@ script:
after_script: set +e
cache:
cargo: true
directories:
- $HOME/.xargo
cache: cargo
before_cache:
# Travis can't cache files that are not readable by "others"

View File

@ -1,21 +1,9 @@
set -euxo pipefail
main() {
# install latest Xargo
local tag=$(git ls-remote --tags --refs --exit-code https://github.com/japaric/xargo \
| cut -d/ -f3 \
| grep -E '^v[0-9.]+$' \
| sort --version-sort \
| tail -n1)
curl -LSfs https://japaric.github.io/trust/install.sh | \
sh -s -- \
--force \
--git japaric/xargo \
--target x86_64-unknown-linux-musl \
--tag $tag
rustup component list | grep 'rust-src.*installed' || \
rustup component add rust-src
if [ $TARGET != x86_64-unknown-linux-gnu ]; then
rustup target add $TARGET
fi
}
main

View File

@ -1,13 +1,9 @@
set -euxo pipefail
main() {
case $TARGET in
thumb*m-none-eabi)
xargo check --target $TARGET
;;
x86_64-unknown-linux-gnu)
cargo check --target $TARGET
if [ $TARGET = x86_64-unknown-linux-gnu ]; then
cargo test --target $TARGET
cargo test --target $TARGET --release
@ -17,12 +13,7 @@ main() {
cargo test --test tsan --target $TARGET
cargo test --test tsan --target $TARGET --release
;;
*)
# unhandled case
exit 1
;;
esac
fi
}
main

View File

@ -113,7 +113,6 @@
#![deny(warnings)]
#![feature(const_fn)]
#![feature(core_intrinsics)]
#![feature(conservative_impl_trait)]
#![feature(unsize)]
#![no_std]
@ -124,11 +123,11 @@ pub use ring_buffer::RingBuffer;
pub use string::String;
pub use vec::Vec;
mod linear_map;
mod cfail;
mod linear_map;
pub mod ring_buffer;
mod string;
mod vec;
pub mod ring_buffer;
/// Error raised when the buffer is full
#[derive(Clone, Copy, Debug, Eq, PartialEq)]

View File

@ -1,15 +1,15 @@
use core::marker::{PhantomData, Unsize};
use core::ptr::{self, NonNull};
use BufferFullError;
use ring_buffer::RingBuffer;
use BufferFullError;
impl<T, A> RingBuffer<T, A>
where
A: Unsize<[T]>,
{
/// Splits a statically allocated ring buffer into producer and consumer end points
pub fn split(&mut self) -> (Producer<T, A>, Consumer<T, A>) {
pub fn split<'rb>(&'rb mut self) -> (Producer<'rb, T, A>, Consumer<'rb, T, A>) {
(
Producer {
rb: unsafe { NonNull::new_unchecked(self) },

View File

@ -1,6 +1,6 @@
use core::marker::Unsize;
use core::{fmt, ops, str};
use core::str::Utf8Error;
use core::{fmt, ops, str};
use {BufferFullError, Vec};
@ -462,15 +462,18 @@ where
macro_rules! impl_eq {
($lhs:ty, $rhs:ty) => {
impl<'a, 'b, A> PartialEq<$rhs> for $lhs
where
A: Unsize<[u8]>,
{
#[inline]
fn eq(&self, other: &$rhs) -> bool { PartialEq::eq(&self[..], &other[..]) }
fn eq(&self, other: &$rhs) -> bool {
PartialEq::eq(&self[..], &other[..])
}
#[inline]
fn ne(&self, other: &$rhs) -> bool { PartialEq::ne(&self[..], &other[..]) }
fn ne(&self, other: &$rhs) -> bool {
PartialEq::ne(&self[..], &other[..])
}
}
impl<'a, 'b, A> PartialEq<$lhs> for $rhs
@ -478,12 +481,15 @@ macro_rules! impl_eq {
A: Unsize<[u8]>,
{
#[inline]
fn eq(&self, other: &$lhs) -> bool { PartialEq::eq(&self[..], &other[..]) }
fn eq(&self, other: &$lhs) -> bool {
PartialEq::eq(&self[..], &other[..])
}
#[inline]
fn ne(&self, other: &$lhs) -> bool { PartialEq::ne(&self[..], &other[..]) }
fn ne(&self, other: &$lhs) -> bool {
PartialEq::ne(&self[..], &other[..])
}
}
};
}
impl_eq! { String<A>, str }

View File

@ -261,7 +261,9 @@ where
fn deref(&self) -> &[T] {
let buffer: &[T] = unsafe { self.buffer.as_ref() };
&buffer[..self.len]
// NOTE(unsafe) avoid bound checks in the slicing operation
// &buffer[..self.len]
unsafe { slice::from_raw_parts(buffer.as_ptr(), self.len) }
}
}
@ -272,7 +274,9 @@ where
fn deref_mut(&mut self) -> &mut [T] {
let len = self.len();
let buffer: &mut [T] = unsafe { self.buffer.as_mut() };
&mut buffer[..len]
// NOTE(unsafe) avoid bound checks in the slicing operation
// &mut buffer[..len]
unsafe { slice::from_raw_parts_mut(buffer.as_mut_ptr(), len) }
}
}