Skip to content

Commit 88b322c

Browse files
committed
native: Remove UnsafeArc in favor of just Arc
1 parent 4c8a4d2 commit 88b322c

File tree

6 files changed

+30
-43
lines changed

6 files changed

+30
-43
lines changed

src/libnative/io/file_unix.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010

1111
//! Blocking posix-based file I/O
1212
13+
use alloc::arc::Arc;
1314
use libc::{c_int, c_void};
1415
use libc;
1516
use std::c_str::CString;
1617
use std::io::IoError;
1718
use std::io;
1819
use std::mem;
1920
use std::rt::rtio;
20-
use std::sync::arc::UnsafeArc;
2121

2222
use io::{IoResult, retry, keep_going};
2323

@@ -29,7 +29,7 @@ struct Inner {
2929
}
3030

3131
pub struct FileDesc {
32-
inner: UnsafeArc<Inner>
32+
inner: Arc<Inner>
3333
}
3434

3535
impl FileDesc {
@@ -42,7 +42,7 @@ impl FileDesc {
4242
/// Note that all I/O operations done on this object will be *blocking*, but
4343
/// they do not require the runtime to be active.
4444
pub fn new(fd: fd_t, close_on_drop: bool) -> FileDesc {
45-
FileDesc { inner: UnsafeArc::new(Inner {
45+
FileDesc { inner: Arc::new(Inner {
4646
fd: fd,
4747
close_on_drop: close_on_drop
4848
}) }
@@ -79,11 +79,7 @@ impl FileDesc {
7979
}
8080
}
8181

82-
pub fn fd(&self) -> fd_t {
83-
// This unsafety is fine because we're just reading off the file
84-
// descriptor, no one is modifying this.
85-
unsafe { (*self.inner.get()).fd }
86-
}
82+
pub fn fd(&self) -> fd_t { self.inner.fd }
8783
}
8884

8985
impl io::Reader for FileDesc {

src/libnative/io/file_win32.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@
1010

1111
//! Blocking win32-based file I/O
1212
13+
use alloc::arc::Arc;
14+
use libc::{c_int, c_void};
15+
use libc;
1316
use std::c_str::CString;
1417
use std::io::IoError;
1518
use std::io;
16-
use libc::{c_int, c_void};
17-
use libc;
1819
use std::mem;
1920
use std::os::win32::{as_utf16_p, fill_utf16_buf_and_decode};
2021
use std::ptr;
2122
use std::rt::rtio;
2223
use std::str;
23-
use std::sync::arc::UnsafeArc;
2424
use std::vec;
2525

2626
use io::IoResult;
@@ -33,7 +33,7 @@ struct Inner {
3333
}
3434

3535
pub struct FileDesc {
36-
inner: UnsafeArc<Inner>
36+
inner: Arc<Inner>
3737
}
3838

3939
impl FileDesc {
@@ -46,7 +46,7 @@ impl FileDesc {
4646
/// Note that all I/O operations done on this object will be *blocking*, but
4747
/// they do not require the runtime to be active.
4848
pub fn new(fd: fd_t, close_on_drop: bool) -> FileDesc {
49-
FileDesc { inner: UnsafeArc::new(Inner {
49+
FileDesc { inner: Arc::new(Inner {
5050
fd: fd,
5151
close_on_drop: close_on_drop
5252
}) }
@@ -85,11 +85,7 @@ impl FileDesc {
8585
Ok(())
8686
}
8787

88-
pub fn fd(&self) -> fd_t {
89-
// This unsafety is fine because we're just reading off the file
90-
// descriptor, no one is modifying this.
91-
unsafe { (*self.inner.get()).fd }
92-
}
88+
pub fn fd(&self) -> fd_t { self.inner.fd }
9389

9490
pub fn handle(&self) -> libc::HANDLE {
9591
unsafe { libc::get_osfhandle(self.fd()) as libc::HANDLE }

src/libnative/io/net.rs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
use alloc::arc::Arc;
1112
use libc;
1213
use std::io::net::ip;
1314
use std::io;
1415
use std::mem;
1516
use std::rt::rtio;
16-
use std::sync::arc::UnsafeArc;
1717
use std::unstable::mutex;
1818

1919
use super::{IoResult, retry, keep_going};
@@ -235,7 +235,7 @@ pub fn init() {
235235
////////////////////////////////////////////////////////////////////////////////
236236

237237
pub struct TcpStream {
238-
inner: UnsafeArc<Inner>,
238+
inner: Arc<Inner>,
239239
read_deadline: u64,
240240
write_deadline: u64,
241241
}
@@ -282,16 +282,13 @@ impl TcpStream {
282282

283283
fn new(inner: Inner) -> TcpStream {
284284
TcpStream {
285-
inner: UnsafeArc::new(inner),
285+
inner: Arc::new(inner),
286286
read_deadline: 0,
287287
write_deadline: 0,
288288
}
289289
}
290290

291-
pub fn fd(&self) -> sock_t {
292-
// This unsafety is fine because it's just a read-only arc
293-
unsafe { (*self.inner.get()).fd }
294-
}
291+
pub fn fd(&self) -> sock_t { self.inner.fd }
295292

296293
fn set_nodelay(&mut self, nodelay: bool) -> IoResult<()> {
297294
setsockopt(self.fd(), libc::IPPROTO_TCP, libc::TCP_NODELAY,
@@ -536,7 +533,7 @@ impl rtio::RtioTcpAcceptor for TcpAcceptor {
536533
////////////////////////////////////////////////////////////////////////////////
537534

538535
pub struct UdpSocket {
539-
inner: UnsafeArc<Inner>,
536+
inner: Arc<Inner>,
540537
read_deadline: u64,
541538
write_deadline: u64,
542539
}
@@ -545,7 +542,7 @@ impl UdpSocket {
545542
pub fn bind(addr: ip::SocketAddr) -> IoResult<UdpSocket> {
546543
let fd = try!(socket(addr, libc::SOCK_DGRAM));
547544
let ret = UdpSocket {
548-
inner: UnsafeArc::new(Inner::new(fd)),
545+
inner: Arc::new(Inner::new(fd)),
549546
read_deadline: 0,
550547
write_deadline: 0,
551548
};
@@ -560,10 +557,7 @@ impl UdpSocket {
560557
}
561558
}
562559

563-
pub fn fd(&self) -> sock_t {
564-
// unsafety is fine because it's just a read-only arc
565-
unsafe { (*self.inner.get()).fd }
566-
}
560+
pub fn fd(&self) -> sock_t { self.inner.fd }
567561

568562
pub fn set_broadcast(&mut self, on: bool) -> IoResult<()> {
569563
setsockopt(self.fd(), libc::SOL_SOCKET, libc::SO_BROADCAST,

src/libnative/io/pipe_unix.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
use alloc::arc::Arc;
1112
use libc;
1213
use std::c_str::CString;
1314
use std::intrinsics;
1415
use std::io;
1516
use std::mem;
1617
use std::rt::rtio;
17-
use std::sync::arc::UnsafeArc;
1818
use std::unstable::mutex;
1919

2020
use super::{IoResult, retry};
@@ -108,7 +108,7 @@ fn bind(addr: &CString, ty: libc::c_int) -> IoResult<Inner> {
108108
////////////////////////////////////////////////////////////////////////////////
109109

110110
pub struct UnixStream {
111-
inner: UnsafeArc<Inner>,
111+
inner: Arc<Inner>,
112112
read_deadline: u64,
113113
write_deadline: u64,
114114
}
@@ -117,19 +117,19 @@ impl UnixStream {
117117
pub fn connect(addr: &CString,
118118
timeout: Option<u64>) -> IoResult<UnixStream> {
119119
connect(addr, libc::SOCK_STREAM, timeout).map(|inner| {
120-
UnixStream::new(UnsafeArc::new(inner))
120+
UnixStream::new(Arc::new(inner))
121121
})
122122
}
123123

124-
fn new(inner: UnsafeArc<Inner>) -> UnixStream {
124+
fn new(inner: Arc<Inner>) -> UnixStream {
125125
UnixStream {
126126
inner: inner,
127127
read_deadline: 0,
128128
write_deadline: 0,
129129
}
130130
}
131131

132-
fn fd(&self) -> fd_t { unsafe { (*self.inner.get()).fd } }
132+
fn fd(&self) -> fd_t { self.inner.fd }
133133

134134
#[cfg(target_os = "linux")]
135135
fn lock_nonblocking(&self) {}
@@ -138,7 +138,7 @@ impl UnixStream {
138138
fn lock_nonblocking<'a>(&'a self) -> net::Guard<'a> {
139139
let ret = net::Guard {
140140
fd: self.fd(),
141-
guard: unsafe { (*self.inner.get()).lock.lock() },
141+
guard: self.inner.lock.lock(),
142142
};
143143
assert!(util::set_nonblocking(self.fd(), true).is_ok());
144144
ret
@@ -254,7 +254,7 @@ impl UnixAcceptor {
254254
&mut size as *mut libc::socklen_t) as libc::c_int
255255
}) {
256256
-1 => Err(super::last_error()),
257-
fd => Ok(UnixStream::new(UnsafeArc::new(Inner::new(fd))))
257+
fd => Ok(UnixStream::new(Arc::new(Inner::new(fd))))
258258
}
259259
}
260260
}

src/libnative/io/pipe_win32.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
//! the test suite passing (the suite is in libstd), and that's good enough for
8585
//! me!
8686
87+
use alloc::arc::Arc;
8788
use libc;
8889
use std::c_str::CString;
8990
use std::intrinsics;
@@ -92,7 +93,6 @@ use std::os::win32::as_utf16_p;
9293
use std::os;
9394
use std::ptr;
9495
use std::rt::rtio;
95-
use std::sync::arc::UnsafeArc;
9696
use std::sync::atomics;
9797
use std::unstable::mutex;
9898

@@ -195,7 +195,7 @@ pub fn await(handle: libc::HANDLE, deadline: u64,
195195
////////////////////////////////////////////////////////////////////////////////
196196

197197
pub struct UnixStream {
198-
inner: UnsafeArc<Inner>,
198+
inner: Arc<Inner>,
199199
write: Option<Event>,
200200
read: Option<Event>,
201201
read_deadline: u64,
@@ -273,7 +273,7 @@ impl UnixStream {
273273
Err(super::last_error())
274274
} else {
275275
Ok(UnixStream {
276-
inner: UnsafeArc::new(inner),
276+
inner: Arc::new(inner),
277277
read: None,
278278
write: None,
279279
read_deadline: 0,
@@ -317,7 +317,7 @@ impl UnixStream {
317317
})
318318
}
319319

320-
fn handle(&self) -> libc::HANDLE { unsafe { (*self.inner.get()).handle } }
320+
fn handle(&self) -> libc::HANDLE { self.inner.handle }
321321

322322
fn read_closed(&self) -> bool {
323323
unsafe { (*self.inner.get()).read_closed.load(atomics::SeqCst) }
@@ -683,7 +683,7 @@ impl UnixAcceptor {
683683

684684
// Transfer ownership of our handle into this stream
685685
Ok(UnixStream {
686-
inner: UnsafeArc::new(Inner::new(handle)),
686+
inner: Arc::new(Inner::new(handle)),
687687
read: None,
688688
write: None,
689689
read_deadline: 0,

src/libnative/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
// answer is that you don't need them)
5858
#![feature(macro_rules)]
5959

60+
extern crate alloc;
6061
extern crate libc;
6162

6263
use std::os;

0 commit comments

Comments
 (0)