elide bounds check in Vec.deref{,_mut}

This commit is contained in:
Jorge Aparicio 2018-04-13 16:21:21 +02:00
parent 17162b56fd
commit 9fc5f71799
4 changed files with 25 additions and 16 deletions

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};
@ -461,16 +461,19 @@ where
}
macro_rules! impl_eq {
($lhs:ty, $rhs: ty) => {
($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) }
}
}