diff options
| author | bors <bors@rust-lang.org> | 2014-05-21 17:31:29 -0700 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2014-05-21 17:31:29 -0700 |
| commit | 257a73ce8273d026f2af1a5021ae2d1a4e7b95e5 (patch) | |
| tree | 2f7d66fc0f7de80105252babe27757e7ea94951a /src/libnative | |
| parent | 5f3f0918ad70cd9b0bfcd2f93aea0218ec92fb87 (diff) | |
| parent | fdf935a5249edd0be0f14385a099963e43c7a29b (diff) | |
| download | rust-257a73ce8273d026f2af1a5021ae2d1a4e7b95e5.tar.gz rust-257a73ce8273d026f2af1a5021ae2d1a4e7b95e5.zip | |
auto merge of #14301 : alexcrichton/rust/remove-unsafe-arc, r=brson
This type can be built with `Arc<Unsafe<T>>` now that liballoc exists.
Diffstat (limited to 'src/libnative')
| -rw-r--r-- | src/libnative/io/file_unix.rs | 12 | ||||
| -rw-r--r-- | src/libnative/io/file_win32.rs | 16 | ||||
| -rw-r--r-- | src/libnative/io/net.rs | 24 | ||||
| -rw-r--r-- | src/libnative/io/pipe_unix.rs | 14 | ||||
| -rw-r--r-- | src/libnative/io/pipe_win32.rs | 26 | ||||
| -rw-r--r-- | src/libnative/lib.rs | 1 |
6 files changed, 40 insertions, 53 deletions
diff --git a/src/libnative/io/file_unix.rs b/src/libnative/io/file_unix.rs index 046d2875d55..5e357ec9cca 100644 --- a/src/libnative/io/file_unix.rs +++ b/src/libnative/io/file_unix.rs @@ -10,6 +10,7 @@ //! Blocking posix-based file I/O +use alloc::arc::Arc; use libc::{c_int, c_void}; use libc; use std::c_str::CString; @@ -17,7 +18,6 @@ use std::io::IoError; use std::io; use std::mem; use std::rt::rtio; -use std::sync::arc::UnsafeArc; use io::{IoResult, retry, keep_going}; @@ -29,7 +29,7 @@ struct Inner { } pub struct FileDesc { - inner: UnsafeArc<Inner> + inner: Arc<Inner> } impl FileDesc { @@ -42,7 +42,7 @@ impl FileDesc { /// Note that all I/O operations done on this object will be *blocking*, but /// they do not require the runtime to be active. pub fn new(fd: fd_t, close_on_drop: bool) -> FileDesc { - FileDesc { inner: UnsafeArc::new(Inner { + FileDesc { inner: Arc::new(Inner { fd: fd, close_on_drop: close_on_drop }) } @@ -79,11 +79,7 @@ impl FileDesc { } } - pub fn fd(&self) -> fd_t { - // This unsafety is fine because we're just reading off the file - // descriptor, no one is modifying this. - unsafe { (*self.inner.get()).fd } - } + pub fn fd(&self) -> fd_t { self.inner.fd } } impl io::Reader for FileDesc { diff --git a/src/libnative/io/file_win32.rs b/src/libnative/io/file_win32.rs index 3bfd70a0a8b..9693f772170 100644 --- a/src/libnative/io/file_win32.rs +++ b/src/libnative/io/file_win32.rs @@ -10,17 +10,17 @@ //! Blocking win32-based file I/O +use alloc::arc::Arc; +use libc::{c_int, c_void}; +use libc; use std::c_str::CString; use std::io::IoError; use std::io; -use libc::{c_int, c_void}; -use libc; use std::mem; use std::os::win32::{as_utf16_p, fill_utf16_buf_and_decode}; use std::ptr; use std::rt::rtio; use std::str; -use std::sync::arc::UnsafeArc; use std::vec; use io::IoResult; @@ -33,7 +33,7 @@ struct Inner { } pub struct FileDesc { - inner: UnsafeArc<Inner> + inner: Arc<Inner> } impl FileDesc { @@ -46,7 +46,7 @@ impl FileDesc { /// Note that all I/O operations done on this object will be *blocking*, but /// they do not require the runtime to be active. pub fn new(fd: fd_t, close_on_drop: bool) -> FileDesc { - FileDesc { inner: UnsafeArc::new(Inner { + FileDesc { inner: Arc::new(Inner { fd: fd, close_on_drop: close_on_drop }) } @@ -85,11 +85,7 @@ impl FileDesc { Ok(()) } - pub fn fd(&self) -> fd_t { - // This unsafety is fine because we're just reading off the file - // descriptor, no one is modifying this. - unsafe { (*self.inner.get()).fd } - } + pub fn fd(&self) -> fd_t { self.inner.fd } pub fn handle(&self) -> libc::HANDLE { unsafe { libc::get_osfhandle(self.fd()) as libc::HANDLE } diff --git a/src/libnative/io/net.rs b/src/libnative/io/net.rs index 6ba92009c39..cacd38c2efd 100644 --- a/src/libnative/io/net.rs +++ b/src/libnative/io/net.rs @@ -8,12 +8,12 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +use alloc::arc::Arc; use libc; use std::io::net::ip; use std::io; use std::mem; use std::rt::rtio; -use std::sync::arc::UnsafeArc; use std::unstable::mutex; use super::{IoResult, retry, keep_going}; @@ -235,7 +235,7 @@ pub fn init() { //////////////////////////////////////////////////////////////////////////////// pub struct TcpStream { - inner: UnsafeArc<Inner>, + inner: Arc<Inner>, read_deadline: u64, write_deadline: u64, } @@ -282,16 +282,13 @@ impl TcpStream { fn new(inner: Inner) -> TcpStream { TcpStream { - inner: UnsafeArc::new(inner), + inner: Arc::new(inner), read_deadline: 0, write_deadline: 0, } } - pub fn fd(&self) -> sock_t { - // This unsafety is fine because it's just a read-only arc - unsafe { (*self.inner.get()).fd } - } + pub fn fd(&self) -> sock_t { self.inner.fd } fn set_nodelay(&mut self, nodelay: bool) -> IoResult<()> { setsockopt(self.fd(), libc::IPPROTO_TCP, libc::TCP_NODELAY, @@ -329,7 +326,7 @@ impl TcpStream { fn lock_nonblocking<'a>(&'a self) -> Guard<'a> { let ret = Guard { fd: self.fd(), - guard: unsafe { (*self.inner.get()).lock.lock() }, + guard: unsafe { self.inner.lock.lock() }, }; assert!(util::set_nonblocking(self.fd(), true).is_ok()); ret @@ -536,7 +533,7 @@ impl rtio::RtioTcpAcceptor for TcpAcceptor { //////////////////////////////////////////////////////////////////////////////// pub struct UdpSocket { - inner: UnsafeArc<Inner>, + inner: Arc<Inner>, read_deadline: u64, write_deadline: u64, } @@ -545,7 +542,7 @@ impl UdpSocket { pub fn bind(addr: ip::SocketAddr) -> IoResult<UdpSocket> { let fd = try!(socket(addr, libc::SOCK_DGRAM)); let ret = UdpSocket { - inner: UnsafeArc::new(Inner::new(fd)), + inner: Arc::new(Inner::new(fd)), read_deadline: 0, write_deadline: 0, }; @@ -560,10 +557,7 @@ impl UdpSocket { } } - pub fn fd(&self) -> sock_t { - // unsafety is fine because it's just a read-only arc - unsafe { (*self.inner.get()).fd } - } + pub fn fd(&self) -> sock_t { self.inner.fd } pub fn set_broadcast(&mut self, on: bool) -> IoResult<()> { setsockopt(self.fd(), libc::SOL_SOCKET, libc::SO_BROADCAST, @@ -603,7 +597,7 @@ impl UdpSocket { fn lock_nonblocking<'a>(&'a self) -> Guard<'a> { let ret = Guard { fd: self.fd(), - guard: unsafe { (*self.inner.get()).lock.lock() }, + guard: unsafe { self.inner.lock.lock() }, }; assert!(util::set_nonblocking(self.fd(), true).is_ok()); ret diff --git a/src/libnative/io/pipe_unix.rs b/src/libnative/io/pipe_unix.rs index d66075f4419..a53a58b6cec 100644 --- a/src/libnative/io/pipe_unix.rs +++ b/src/libnative/io/pipe_unix.rs @@ -8,13 +8,13 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +use alloc::arc::Arc; use libc; use std::c_str::CString; use std::intrinsics; use std::io; use std::mem; use std::rt::rtio; -use std::sync::arc::UnsafeArc; use std::unstable::mutex; use super::{IoResult, retry}; @@ -108,7 +108,7 @@ fn bind(addr: &CString, ty: libc::c_int) -> IoResult<Inner> { //////////////////////////////////////////////////////////////////////////////// pub struct UnixStream { - inner: UnsafeArc<Inner>, + inner: Arc<Inner>, read_deadline: u64, write_deadline: u64, } @@ -117,11 +117,11 @@ impl UnixStream { pub fn connect(addr: &CString, timeout: Option<u64>) -> IoResult<UnixStream> { connect(addr, libc::SOCK_STREAM, timeout).map(|inner| { - UnixStream::new(UnsafeArc::new(inner)) + UnixStream::new(Arc::new(inner)) }) } - fn new(inner: UnsafeArc<Inner>) -> UnixStream { + fn new(inner: Arc<Inner>) -> UnixStream { UnixStream { inner: inner, read_deadline: 0, @@ -129,7 +129,7 @@ impl UnixStream { } } - fn fd(&self) -> fd_t { unsafe { (*self.inner.get()).fd } } + fn fd(&self) -> fd_t { self.inner.fd } #[cfg(target_os = "linux")] fn lock_nonblocking(&self) {} @@ -138,7 +138,7 @@ impl UnixStream { fn lock_nonblocking<'a>(&'a self) -> net::Guard<'a> { let ret = net::Guard { fd: self.fd(), - guard: unsafe { (*self.inner.get()).lock.lock() }, + guard: unsafe { self.inner.lock.lock() }, }; assert!(util::set_nonblocking(self.fd(), true).is_ok()); ret @@ -254,7 +254,7 @@ impl UnixAcceptor { &mut size as *mut libc::socklen_t) as libc::c_int }) { -1 => Err(super::last_error()), - fd => Ok(UnixStream::new(UnsafeArc::new(Inner::new(fd)))) + fd => Ok(UnixStream::new(Arc::new(Inner::new(fd)))) } } } diff --git a/src/libnative/io/pipe_win32.rs b/src/libnative/io/pipe_win32.rs index 5acb48a5f39..cd4cbf2c90f 100644 --- a/src/libnative/io/pipe_win32.rs +++ b/src/libnative/io/pipe_win32.rs @@ -84,6 +84,7 @@ //! the test suite passing (the suite is in libstd), and that's good enough for //! me! +use alloc::arc::Arc; use libc; use std::c_str::CString; use std::io; @@ -92,7 +93,6 @@ use std::os::win32::as_utf16_p; use std::os; use std::ptr; use std::rt::rtio; -use std::sync::arc::UnsafeArc; use std::sync::atomics; use std::unstable::mutex; @@ -195,7 +195,7 @@ pub fn await(handle: libc::HANDLE, deadline: u64, //////////////////////////////////////////////////////////////////////////////// pub struct UnixStream { - inner: UnsafeArc<Inner>, + inner: Arc<Inner>, write: Option<Event>, read: Option<Event>, read_deadline: u64, @@ -273,7 +273,7 @@ impl UnixStream { Err(super::last_error()) } else { Ok(UnixStream { - inner: UnsafeArc::new(inner), + inner: Arc::new(inner), read: None, write: None, read_deadline: 0, @@ -317,14 +317,14 @@ impl UnixStream { }) } - fn handle(&self) -> libc::HANDLE { unsafe { (*self.inner.get()).handle } } + fn handle(&self) -> libc::HANDLE { self.inner.handle } fn read_closed(&self) -> bool { - unsafe { (*self.inner.get()).read_closed.load(atomics::SeqCst) } + self.inner.read_closed.load(atomics::SeqCst) } fn write_closed(&self) -> bool { - unsafe { (*self.inner.get()).write_closed.load(atomics::SeqCst) } + self.inner.write_closed.load(atomics::SeqCst) } fn cancel_io(&self) -> IoResult<()> { @@ -353,7 +353,7 @@ impl rtio::RtioPipe for UnixStream { // acquire the lock. // // See comments in close_read() about why this lock is necessary. - let guard = unsafe { (*self.inner.get()).lock.lock() }; + let guard = unsafe { self.inner.lock.lock() }; if self.read_closed() { return Err(io::standard_error(io::EndOfFile)) } @@ -429,7 +429,7 @@ impl rtio::RtioPipe for UnixStream { // going after we woke up. // // See comments in close_read() about why this lock is necessary. - let guard = unsafe { (*self.inner.get()).lock.lock() }; + let guard = unsafe { self.inner.lock.lock() }; if self.write_closed() { return Err(io::standard_error(io::BrokenPipe)) } @@ -514,15 +514,15 @@ impl rtio::RtioPipe for UnixStream { // close_read() between steps 1 and 2. By atomically executing steps 1 // and 2 with a lock with respect to close_read(), we're guaranteed that // no thread will erroneously sit in a read forever. - let _guard = unsafe { (*self.inner.get()).lock.lock() }; - unsafe { (*self.inner.get()).read_closed.store(true, atomics::SeqCst) } + let _guard = unsafe { self.inner.lock.lock() }; + self.inner.read_closed.store(true, atomics::SeqCst); self.cancel_io() } fn close_write(&mut self) -> IoResult<()> { // see comments in close_read() for why this lock is necessary - let _guard = unsafe { (*self.inner.get()).lock.lock() }; - unsafe { (*self.inner.get()).write_closed.store(true, atomics::SeqCst) } + let _guard = unsafe { self.inner.lock.lock() }; + self.inner.write_closed.store(true, atomics::SeqCst); self.cancel_io() } @@ -683,7 +683,7 @@ impl UnixAcceptor { // Transfer ownership of our handle into this stream Ok(UnixStream { - inner: UnsafeArc::new(Inner::new(handle)), + inner: Arc::new(Inner::new(handle)), read: None, write: None, read_deadline: 0, diff --git a/src/libnative/lib.rs b/src/libnative/lib.rs index 8ba06133369..634bf1dcedb 100644 --- a/src/libnative/lib.rs +++ b/src/libnative/lib.rs @@ -57,6 +57,7 @@ // answer is that you don't need them) #![feature(macro_rules)] +extern crate alloc; extern crate libc; use std::os; |
