mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-28 09:42:40 +01:00
Reduced unsafe converage (#520)
This commit is contained in:
parent
1b298142e3
commit
782eeb5ded
@ -209,31 +209,35 @@ impl<T> Node<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn insert<I>(&mut self, next_el: &mut Node<I>) {
|
fn insert<I>(&mut self, next_el: &mut Node<I>) {
|
||||||
unsafe {
|
|
||||||
let next: *mut Node<T> = next_el as *const _ as *mut _;
|
let next: *mut Node<T> = next_el as *const _ as *mut _;
|
||||||
|
|
||||||
if let Some(next2) = self.next {
|
if let Some(next2) = self.next {
|
||||||
|
unsafe {
|
||||||
let n = next2.as_mut().unwrap();
|
let n = next2.as_mut().unwrap();
|
||||||
n.prev = Some(next);
|
n.prev = Some(next);
|
||||||
|
}
|
||||||
next_el.next = Some(next2 as *mut _);
|
next_el.next = Some(next2 as *mut _);
|
||||||
}
|
}
|
||||||
self.next = Some(next);
|
self.next = Some(next);
|
||||||
|
|
||||||
|
unsafe {
|
||||||
let next: &mut Node<T> = &mut *next;
|
let next: &mut Node<T> = &mut *next;
|
||||||
next.prev = Some(self as *mut _);
|
next.prev = Some(self as *mut _);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn remove(&mut self) {
|
fn remove(&mut self) {
|
||||||
unsafe {
|
|
||||||
self.element = ptr::null_mut();
|
self.element = ptr::null_mut();
|
||||||
let next = self.next.take();
|
let next = self.next.take();
|
||||||
let prev = self.prev.take();
|
let prev = self.prev.take();
|
||||||
|
|
||||||
if let Some(prev) = prev {
|
if let Some(prev) = prev {
|
||||||
|
unsafe {
|
||||||
prev.as_mut().unwrap().next = next;
|
prev.as_mut().unwrap().next = next;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if let Some(next) = next {
|
if let Some(next) = next {
|
||||||
|
unsafe {
|
||||||
next.as_mut().unwrap().prev = prev;
|
next.as_mut().unwrap().prev = prev;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -196,8 +196,7 @@ impl<T: AsyncWrite, H: 'static> Writer for H1Writer<T, H> {
|
|||||||
let mut pos = 0;
|
let mut pos = 0;
|
||||||
let mut has_date = false;
|
let mut has_date = false;
|
||||||
let mut remaining = buffer.remaining_mut();
|
let mut remaining = buffer.remaining_mut();
|
||||||
unsafe {
|
let mut buf = unsafe { &mut *(buffer.bytes_mut() as *mut [u8]) };
|
||||||
let mut buf = &mut *(buffer.bytes_mut() as *mut [u8]);
|
|
||||||
for (key, value) in msg.headers() {
|
for (key, value) in msg.headers() {
|
||||||
match *key {
|
match *key {
|
||||||
TRANSFER_ENCODING => continue,
|
TRANSFER_ENCODING => continue,
|
||||||
@ -218,12 +217,16 @@ impl<T: AsyncWrite, H: 'static> Writer for H1Writer<T, H> {
|
|||||||
let k = key.as_str().as_bytes();
|
let k = key.as_str().as_bytes();
|
||||||
let len = k.len() + v.len() + 4;
|
let len = k.len() + v.len() + 4;
|
||||||
if len > remaining {
|
if len > remaining {
|
||||||
|
unsafe {
|
||||||
buffer.advance_mut(pos);
|
buffer.advance_mut(pos);
|
||||||
|
}
|
||||||
pos = 0;
|
pos = 0;
|
||||||
buffer.reserve(len);
|
buffer.reserve(len);
|
||||||
remaining = buffer.remaining_mut();
|
remaining = buffer.remaining_mut();
|
||||||
|
unsafe {
|
||||||
buf = &mut *(buffer.bytes_mut() as *mut _);
|
buf = &mut *(buffer.bytes_mut() as *mut _);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
buf[pos..pos + k.len()].copy_from_slice(k);
|
buf[pos..pos + k.len()].copy_from_slice(k);
|
||||||
pos += k.len();
|
pos += k.len();
|
||||||
@ -235,6 +238,7 @@ impl<T: AsyncWrite, H: 'static> Writer for H1Writer<T, H> {
|
|||||||
pos += 2;
|
pos += 2;
|
||||||
remaining -= len;
|
remaining -= len;
|
||||||
}
|
}
|
||||||
|
unsafe {
|
||||||
buffer.advance_mut(pos);
|
buffer.advance_mut(pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,20 +29,24 @@ pub(crate) fn write_status_line(version: Version, mut n: u16, bytes: &mut BytesM
|
|||||||
let lut_ptr = DEC_DIGITS_LUT.as_ptr();
|
let lut_ptr = DEC_DIGITS_LUT.as_ptr();
|
||||||
let four = n > 999;
|
let four = n > 999;
|
||||||
|
|
||||||
unsafe {
|
|
||||||
// decode 2 more chars, if > 2 chars
|
// decode 2 more chars, if > 2 chars
|
||||||
let d1 = (n % 100) << 1;
|
let d1 = (n % 100) << 1;
|
||||||
n /= 100;
|
n /= 100;
|
||||||
curr -= 2;
|
curr -= 2;
|
||||||
|
unsafe {
|
||||||
ptr::copy_nonoverlapping(lut_ptr.offset(d1 as isize), buf_ptr.offset(curr), 2);
|
ptr::copy_nonoverlapping(lut_ptr.offset(d1 as isize), buf_ptr.offset(curr), 2);
|
||||||
|
}
|
||||||
|
|
||||||
// decode last 1 or 2 chars
|
// decode last 1 or 2 chars
|
||||||
if n < 10 {
|
if n < 10 {
|
||||||
curr -= 1;
|
curr -= 1;
|
||||||
|
unsafe {
|
||||||
*buf_ptr.offset(curr) = (n as u8) + b'0';
|
*buf_ptr.offset(curr) = (n as u8) + b'0';
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
let d1 = n << 1;
|
let d1 = n << 1;
|
||||||
curr -= 2;
|
curr -= 2;
|
||||||
|
unsafe {
|
||||||
ptr::copy_nonoverlapping(
|
ptr::copy_nonoverlapping(
|
||||||
lut_ptr.offset(d1 as isize),
|
lut_ptr.offset(d1 as isize),
|
||||||
buf_ptr.offset(curr),
|
buf_ptr.offset(curr),
|
||||||
@ -107,9 +111,8 @@ pub fn write_content_length(mut n: usize, bytes: &mut BytesMut) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn convert_usize(mut n: usize, bytes: &mut BytesMut) {
|
pub(crate) fn convert_usize(mut n: usize, bytes: &mut BytesMut) {
|
||||||
unsafe {
|
|
||||||
let mut curr: isize = 39;
|
let mut curr: isize = 39;
|
||||||
let mut buf: [u8; 41] = mem::uninitialized();
|
let mut buf: [u8; 41] = unsafe { mem::uninitialized() };
|
||||||
buf[39] = b'\r';
|
buf[39] = b'\r';
|
||||||
buf[40] = b'\n';
|
buf[40] = b'\n';
|
||||||
let buf_ptr = buf.as_mut_ptr();
|
let buf_ptr = buf.as_mut_ptr();
|
||||||
@ -123,9 +126,11 @@ pub(crate) fn convert_usize(mut n: usize, bytes: &mut BytesMut) {
|
|||||||
let d1 = (rem / 100) << 1;
|
let d1 = (rem / 100) << 1;
|
||||||
let d2 = (rem % 100) << 1;
|
let d2 = (rem % 100) << 1;
|
||||||
curr -= 4;
|
curr -= 4;
|
||||||
|
unsafe {
|
||||||
ptr::copy_nonoverlapping(lut_ptr.offset(d1), buf_ptr.offset(curr), 2);
|
ptr::copy_nonoverlapping(lut_ptr.offset(d1), buf_ptr.offset(curr), 2);
|
||||||
ptr::copy_nonoverlapping(lut_ptr.offset(d2), buf_ptr.offset(curr + 2), 2);
|
ptr::copy_nonoverlapping(lut_ptr.offset(d2), buf_ptr.offset(curr + 2), 2);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// if we reach here numbers are <= 9999, so at most 4 chars long
|
// if we reach here numbers are <= 9999, so at most 4 chars long
|
||||||
let mut n = n as isize; // possibly reduce 64bit math
|
let mut n = n as isize; // possibly reduce 64bit math
|
||||||
@ -135,19 +140,26 @@ pub(crate) fn convert_usize(mut n: usize, bytes: &mut BytesMut) {
|
|||||||
let d1 = (n % 100) << 1;
|
let d1 = (n % 100) << 1;
|
||||||
n /= 100;
|
n /= 100;
|
||||||
curr -= 2;
|
curr -= 2;
|
||||||
|
unsafe {
|
||||||
ptr::copy_nonoverlapping(lut_ptr.offset(d1), buf_ptr.offset(curr), 2);
|
ptr::copy_nonoverlapping(lut_ptr.offset(d1), buf_ptr.offset(curr), 2);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// decode last 1 or 2 chars
|
// decode last 1 or 2 chars
|
||||||
if n < 10 {
|
if n < 10 {
|
||||||
curr -= 1;
|
curr -= 1;
|
||||||
|
unsafe {
|
||||||
*buf_ptr.offset(curr) = (n as u8) + b'0';
|
*buf_ptr.offset(curr) = (n as u8) + b'0';
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
let d1 = n << 1;
|
let d1 = n << 1;
|
||||||
curr -= 2;
|
curr -= 2;
|
||||||
|
unsafe {
|
||||||
ptr::copy_nonoverlapping(lut_ptr.offset(d1), buf_ptr.offset(curr), 2);
|
ptr::copy_nonoverlapping(lut_ptr.offset(d1), buf_ptr.offset(curr), 2);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
unsafe {
|
||||||
bytes.extend_from_slice(slice::from_raw_parts(
|
bytes.extend_from_slice(slice::from_raw_parts(
|
||||||
buf_ptr.offset(curr),
|
buf_ptr.offset(curr),
|
||||||
41 - curr as usize,
|
41 - curr as usize,
|
||||||
|
@ -396,16 +396,19 @@ pub trait IoStream: AsyncRead + AsyncWrite + 'static {
|
|||||||
if buf.remaining_mut() < LW_BUFFER_SIZE {
|
if buf.remaining_mut() < LW_BUFFER_SIZE {
|
||||||
buf.reserve(HW_BUFFER_SIZE);
|
buf.reserve(HW_BUFFER_SIZE);
|
||||||
}
|
}
|
||||||
unsafe {
|
|
||||||
match self.read(buf.bytes_mut()) {
|
let read = unsafe { self.read(buf.bytes_mut()) };
|
||||||
|
match read {
|
||||||
Ok(n) => {
|
Ok(n) => {
|
||||||
if n == 0 {
|
if n == 0 {
|
||||||
return Ok(Async::Ready((read_some, true)));
|
return Ok(Async::Ready((read_some, true)));
|
||||||
} else {
|
} else {
|
||||||
read_some = true;
|
read_some = true;
|
||||||
|
unsafe {
|
||||||
buf.advance_mut(n);
|
buf.advance_mut(n);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
return if e.kind() == io::ErrorKind::WouldBlock {
|
return if e.kind() == io::ErrorKind::WouldBlock {
|
||||||
if read_some {
|
if read_some {
|
||||||
@ -420,7 +423,6 @@ pub trait IoStream: AsyncRead + AsyncWrite + 'static {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/// Extra io stream extensions
|
/// Extra io stream extensions
|
||||||
fn extensions(&self) -> Option<Rc<Extensions>> {
|
fn extensions(&self) -> Option<Rc<Extensions>> {
|
||||||
|
@ -148,7 +148,7 @@ impl Quoter {
|
|||||||
if let Some(data) = cloned {
|
if let Some(data) = cloned {
|
||||||
// Unsafe: we get data from http::Uri, which does utf-8 checks already
|
// Unsafe: we get data from http::Uri, which does utf-8 checks already
|
||||||
// this code only decodes valid pct encoded values
|
// this code only decodes valid pct encoded values
|
||||||
Some(unsafe { Rc::new(String::from_utf8_unchecked(data)) })
|
Some(Rc::new(unsafe { String::from_utf8_unchecked(data) }))
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user