From 362b14c2f734c33a7b96cc21ba82b50b7767c2eb Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Tue, 19 Jun 2018 09:36:17 +0600 Subject: [PATCH] remove unsafe cell from ws client --- src/client/pipeline.rs | 6 +++--- src/extensions.rs | 1 - src/ws/client.rs | 12 ++++++------ 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/client/pipeline.rs b/src/client/pipeline.rs index c2caf83cf..de1ff1287 100644 --- a/src/client/pipeline.rs +++ b/src/client/pipeline.rs @@ -317,9 +317,6 @@ impl Pipeline { if self.conn.is_none() { return Ok(Async::Ready(None)); } - let conn: &mut Connection = - unsafe { &mut *(self.conn.as_mut().unwrap() as *mut _) }; - let mut need_run = false; // need write? @@ -337,6 +334,9 @@ impl Pipeline { // need read? if self.parser.is_some() { + let conn: &mut Connection = + unsafe { &mut *(self.conn.as_mut().unwrap() as *mut _) }; + loop { match self .parser diff --git a/src/extensions.rs b/src/extensions.rs index 1c64623f2..7fdd142b2 100644 --- a/src/extensions.rs +++ b/src/extensions.rs @@ -48,7 +48,6 @@ impl Extensions { /// If a extension of this type existed, it will be returned. pub fn remove(&mut self) -> Option { self.map.remove(&TypeId::of::()).and_then(|boxed| { - //TODO: we can use unsafe and remove double checking the type id (boxed as Box) .downcast() .ok() diff --git a/src/ws/client.rs b/src/ws/client.rs index 7cf0095d6..5087c885a 100644 --- a/src/ws/client.rs +++ b/src/ws/client.rs @@ -1,5 +1,5 @@ //! Http client request -use std::cell::UnsafeCell; +use std::cell::RefCell; use std::rc::Rc; use std::time::Duration; use std::{fmt, io, str}; @@ -422,7 +422,7 @@ impl Future for ClientHandshake { closed: false, }; - let inner = Rc::new(UnsafeCell::new(inner)); + let inner = Rc::new(RefCell::new(inner)); Ok(Async::Ready(( ClientReader { inner: Rc::clone(&inner), @@ -435,7 +435,7 @@ impl Future for ClientHandshake { /// Websocket reader client pub struct ClientReader { - inner: Rc>, + inner: Rc>, max_size: usize, } @@ -451,7 +451,7 @@ impl Stream for ClientReader { fn poll(&mut self) -> Poll, Self::Error> { let max_size = self.max_size; - let inner: &mut Inner = unsafe { &mut *self.inner.get() }; + let mut inner = self.inner.borrow_mut(); if inner.closed { return Ok(Async::Ready(None)); } @@ -507,14 +507,14 @@ impl Stream for ClientReader { /// Websocket writer client pub struct ClientWriter { - inner: Rc>, + inner: Rc>, } impl ClientWriter { /// Write payload #[inline] fn write(&mut self, mut data: Binary) { - let inner: &mut Inner = unsafe { &mut *self.inner.get() }; + let inner = self.inner.borrow_mut(); if !inner.closed { let _ = inner.tx.unbounded_send(data.take()); } else {