1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-30 18:44:35 +01:00

cleanup more code

This commit is contained in:
Nikolay Kim 2018-06-20 00:36:32 +06:00
parent a69c1e3de5
commit 311f0b23a9
4 changed files with 11 additions and 13 deletions

View File

@ -10,7 +10,7 @@ use std::{cmp, io};
#[cfg(unix)] #[cfg(unix)]
use std::os::unix::fs::MetadataExt; use std::os::unix::fs::MetadataExt;
use bytes::{BufMut, Bytes, BytesMut}; use bytes::Bytes;
use futures::{Async, Future, Poll, Stream}; use futures::{Async, Future, Poll, Stream};
use futures_cpupool::{CpuFuture, CpuPool}; use futures_cpupool::{CpuFuture, CpuPool};
use mime; use mime;
@ -439,14 +439,13 @@ impl Stream for ChunkedReadFile {
self.fut = Some(self.cpu_pool.spawn_fn(move || { self.fut = Some(self.cpu_pool.spawn_fn(move || {
let max_bytes: usize; let max_bytes: usize;
max_bytes = cmp::min(size.saturating_sub(counter), 65_536) as usize; max_bytes = cmp::min(size.saturating_sub(counter), 65_536) as usize;
let mut buf = BytesMut::from(Vec::with_capacity(max_bytes)); let mut buf = Vec::with_capacity(max_bytes);
file.seek(io::SeekFrom::Start(offset))?; file.seek(io::SeekFrom::Start(offset))?;
let nbytes = file.read(unsafe { buf.bytes_mut() })?; let nbytes = file.read(buf.as_mut_slice())?;
if nbytes == 0 { if nbytes == 0 {
return Err(io::ErrorKind::UnexpectedEof.into()); return Err(io::ErrorKind::UnexpectedEof.into());
} }
unsafe { buf.advance_mut(nbytes) }; Ok((file, Bytes::from(buf)))
Ok((file, buf.freeze()))
})); }));
self.poll() self.poll()
} }

View File

@ -634,13 +634,11 @@ mod tests {
assert!(req.chunked().unwrap()); assert!(req.chunked().unwrap());
let mut headers = HeaderMap::new(); let mut headers = HeaderMap::new();
let s = unsafe { let hdr = Bytes::from_static(b"some va\xadscc\xacas0xsdasdlue");
str::from_utf8_unchecked(b"some va\xadscc\xacas0xsdasdlue".as_ref())
};
headers.insert( headers.insert(
header::TRANSFER_ENCODING, header::TRANSFER_ENCODING,
header::HeaderValue::from_str(s).unwrap(), header::HeaderValue::from_shared(hdr).unwrap(),
); );
let req = HttpRequest::new( let req = HttpRequest::new(
Method::GET, Method::GET,

View File

@ -1,9 +1,8 @@
//! HTTP Request message related code. //! HTTP Request message related code.
#![cfg_attr(feature = "cargo-clippy", allow(transmute_ptr_to_ptr))]
use std::collections::HashMap; use std::collections::HashMap;
use std::net::SocketAddr; use std::net::SocketAddr;
use std::rc::Rc; use std::rc::Rc;
use std::{cmp, fmt, io, mem, str}; use std::{cmp, fmt, io, str};
use bytes::Bytes; use bytes::Bytes;
use cookie::Cookie; use cookie::Cookie;
@ -446,13 +445,13 @@ impl<S> HttpRequest<S> {
/// access the matched value for that segment. /// access the matched value for that segment.
#[inline] #[inline]
pub fn match_info(&self) -> &Params { pub fn match_info(&self) -> &Params {
unsafe { mem::transmute(&self.as_ref().params) } &self.as_ref().params
} }
/// Get mutable reference to request's Params. /// Get mutable reference to request's Params.
#[inline] #[inline]
pub(crate) fn match_info_mut(&mut self) -> &mut Params { pub(crate) fn match_info_mut(&mut self) -> &mut Params {
unsafe { mem::transmute(&mut self.as_mut().params) } &mut self.as_mut().params
} }
/// Checks if a connection should be kept alive. /// Checks if a connection should be kept alive.

View File

@ -146,6 +146,8 @@ impl Quoter {
} }
if let Some(data) = cloned { if let Some(data) = cloned {
// we get data from http::Uri, which does utf-8 checks already
// this code only decodes valid pct encoded values
Some(unsafe { Rc::new(String::from_utf8_unchecked(data)) }) Some(unsafe { Rc::new(String::from_utf8_unchecked(data)) })
} else { } else {
None None