Skip to content

Commit 2eb4f05

Browse files
committed
Replace C types with Rust types in libstd, closes #7313
1 parent 750d48b commit 2eb4f05

19 files changed

+158
-173
lines changed

src/libstd/cleanup.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@
1010

1111
#[doc(hidden)];
1212

13-
use libc::c_void;
1413
use ptr;
1514
use unstable::intrinsics::TyDesc;
1615
use unstable::raw;
1716

18-
type DropGlue<'a> = 'a |**TyDesc, *c_void|;
17+
type DropGlue<'a> = 'a |**TyDesc, *u8|;
1918

2019
static RC_IMMORTAL : uint = 0x77777777;
2120

@@ -107,7 +106,7 @@ pub unsafe fn annihilate() {
107106
stats.n_bytes_freed +=
108107
(*((*alloc).type_desc)).size
109108
+ mem::size_of::<raw::Box<()>>();
110-
local_free(alloc as *i8);
109+
local_free(alloc as *u8);
111110
true
112111
});
113112

src/libstd/local_data.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,9 @@ local_data::get(key_vector, |opt| assert_eq!(*opt.unwrap(), ~[4]));
4141
// magic.
4242

4343
use cast;
44-
use libc;
4544
use prelude::*;
4645
use rt::task::{Task, LocalStorage};
47-
use util;
46+
use util::replace;
4847

4948
/**
5049
* Indexes a task-local data slot. This pointer is used for comparison to
@@ -87,7 +86,7 @@ impl<T: 'static> LocalData for T {}
8786
// n.b. If TLS is used heavily in future, this could be made more efficient with
8887
// a proper map.
8988
#[doc(hidden)]
90-
pub type Map = ~[Option<(*libc::c_void, TLSValue, LoanState)>];
89+
pub type Map = ~[Option<(*u8, TLSValue, LoanState)>];
9190
type TLSValue = ~LocalData;
9291

9392
// Gets the map from the runtime. Lazily initialises if not done so already.
@@ -128,7 +127,7 @@ impl LoanState {
128127
}
129128
}
130129

131-
fn key_to_key_value<T: 'static>(key: Key<T>) -> *libc::c_void {
130+
fn key_to_key_value<T: 'static>(key: Key<T>) -> *u8 {
132131
unsafe { cast::transmute(key) }
133132
}
134133

@@ -151,7 +150,7 @@ pub fn pop<T: 'static>(key: Key<T>) -> Option<T> {
151150
// Move the data out of the `entry` slot via util::replace.
152151
// This is guaranteed to succeed because we already matched
153152
// on `Some` above.
154-
let data = match util::replace(entry, None) {
153+
let data = match replace(entry, None) {
155154
Some((_, data, _)) => data,
156155
None => abort()
157156
};
@@ -302,7 +301,7 @@ pub fn set<T: 'static>(key: Key<T>, data: T) {
302301
let data = ~data as ~LocalData:;
303302

304303
fn insertion_position(map: &mut Map,
305-
key: *libc::c_void) -> Option<uint> {
304+
key: *u8) -> Option<uint> {
306305
// First see if the map contains this key already
307306
let curspot = map.iter().position(|entry| {
308307
match *entry {

src/libstd/os.rs

+32-32
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ use unstable::finally::Finally;
4444
use sync::atomics::{AtomicInt, INIT_ATOMIC_INT, SeqCst};
4545

4646
/// Delegates to the libc close() function, returning the same return value.
47-
pub fn close(fd: c_int) -> c_int {
47+
pub fn close(fd: int) -> int {
4848
unsafe {
49-
libc::close(fd)
49+
libc::close(fd as c_int) as int
5050
}
5151
}
5252

@@ -57,7 +57,7 @@ static BUF_BYTES : uint = 2048u;
5757
pub fn getcwd() -> Path {
5858
use c_str::CString;
5959

60-
let mut buf = [0 as libc::c_char, ..BUF_BYTES];
60+
let mut buf = [0 as c_char, ..BUF_BYTES];
6161
unsafe {
6262
if libc::getcwd(buf.as_mut_ptr(), buf.len() as size_t).is_null() {
6363
fail!()
@@ -164,7 +164,7 @@ pub fn env() -> ~[(~str,~str)] {
164164
os::last_os_error());
165165
}
166166
let mut result = ~[];
167-
c_str::from_c_multistring(ch as *libc::c_char, None, |cstr| {
167+
c_str::from_c_multistring(ch as *c_char, None, |cstr| {
168168
result.push(cstr.as_str().unwrap().to_owned());
169169
});
170170
FreeEnvironmentStringsA(ch);
@@ -173,7 +173,7 @@ pub fn env() -> ~[(~str,~str)] {
173173
#[cfg(unix)]
174174
unsafe fn get_env_pairs() -> ~[~str] {
175175
extern {
176-
fn rust_env_pairs() -> **libc::c_char;
176+
fn rust_env_pairs() -> **c_char;
177177
}
178178
let environ = rust_env_pairs();
179179
if environ as uint == 0 {
@@ -306,9 +306,9 @@ pub struct Pipe {
306306
#[cfg(unix)]
307307
pub fn pipe() -> Pipe {
308308
unsafe {
309-
let mut fds = Pipe {input: 0 as c_int,
310-
out: 0 as c_int };
311-
assert_eq!(libc::pipe(&mut fds.input), (0 as c_int));
309+
let mut fds = Pipe {input: 0,
310+
out: 0};
311+
assert_eq!(libc::pipe(&mut fds.input), 0);
312312
return Pipe {input: fds.input, out: fds.out};
313313
}
314314
}
@@ -321,13 +321,13 @@ pub fn pipe() -> Pipe {
321321
// fully understand. Here we explicitly make the pipe non-inheritable,
322322
// which means to pass it to a subprocess they need to be duplicated
323323
// first, as in std::run.
324-
let mut fds = Pipe {input: 0 as c_int,
325-
out: 0 as c_int };
324+
let mut fds = Pipe {input: 0,
325+
out: 0};
326326
let res = libc::pipe(&mut fds.input, 1024 as ::libc::c_uint,
327327
(libc::O_BINARY | libc::O_NOINHERIT) as c_int);
328-
assert_eq!(res, 0 as c_int);
329-
assert!((fds.input != -1 as c_int && fds.input != 0 as c_int));
330-
assert!((fds.out != -1 as c_int && fds.input != 0 as c_int));
328+
assert_eq!(res, 0);
329+
assert!((fds.input != -1 && fds.input != 0 ));
330+
assert!((fds.out != -1 && fds.input != 0));
331331
return Pipe {input: fds.input, out: fds.out};
332332
}
333333
}
@@ -699,7 +699,7 @@ pub fn get_exit_status() -> int {
699699
}
700700

701701
#[cfg(target_os = "macos")]
702-
unsafe fn load_argc_and_argv(argc: c_int, argv: **c_char) -> ~[~str] {
702+
unsafe fn load_argc_and_argv(argc: int, argv: **c_char) -> ~[~str] {
703703
let mut args = ~[];
704704
for i in range(0u, argc as uint) {
705705
args.push(str::raw::from_c_str(*argv.offset(i as int)));
@@ -715,7 +715,7 @@ unsafe fn load_argc_and_argv(argc: c_int, argv: **c_char) -> ~[~str] {
715715
#[cfg(target_os = "macos")]
716716
fn real_args() -> ~[~str] {
717717
unsafe {
718-
let (argc, argv) = (*_NSGetArgc() as c_int,
718+
let (argc, argv) = (*_NSGetArgc() as int,
719719
*_NSGetArgv() as **c_char);
720720
load_argc_and_argv(argc, argv)
721721
}
@@ -833,7 +833,7 @@ pub struct MemoryMap {
833833
/// Pointer to the memory created or modified by this map.
834834
data: *mut u8,
835835
/// Number of bytes this map applies to
836-
len: size_t,
836+
len: uint,
837837
/// Type of mapping
838838
kind: MemoryMapKind
839839
}
@@ -842,7 +842,7 @@ pub struct MemoryMap {
842842
pub enum MemoryMapKind {
843843
/// Memory-mapped file. On Windows, the inner pointer is a handle to the mapping, and
844844
/// corresponds to `CreateFileMapping`. Elsewhere, it is null.
845-
MapFile(*c_void),
845+
MapFile(*u8),
846846
/// Virtual memory map. Usually used to change the permissions of a given chunk of memory.
847847
/// Corresponds to `VirtualAlloc` on Windows.
848848
MapVirtual
@@ -857,7 +857,7 @@ pub enum MapOption {
857857
/// The memory should be executable
858858
MapExecutable,
859859
/// Create a map for a specific address range. Corresponds to `MAP_FIXED` on POSIX.
860-
MapAddr(*c_void),
860+
MapAddr(*u8),
861861
/// Create a memory mapping for a file with a given fd.
862862
MapFd(c_int),
863863
/// When using `MapFd`, the start of the map is `uint` bytes from the start of the file.
@@ -881,7 +881,7 @@ pub enum MapError {
881881
/// using `MapFd`, the target of the fd didn't have enough resources to fulfill the request.
882882
ErrNoMem,
883883
/// Unrecognized error. The inner value is the unrecognized errno.
884-
ErrUnknown(libc::c_int),
884+
ErrUnknown(int),
885885
/// ## The following are win32-specific
886886
///
887887
/// Unsupported combination of protection flags (`MapReadable`/`MapWritable`/`MapExecutable`).
@@ -926,12 +926,12 @@ impl MemoryMap {
926926
pub fn new(min_len: uint, options: &[MapOption]) -> Result<MemoryMap, MapError> {
927927
use libc::off_t;
928928

929-
let mut addr: *c_void = ptr::null();
930-
let mut prot: c_int = 0;
931-
let mut flags: c_int = libc::MAP_PRIVATE;
932-
let mut fd: c_int = -1;
933-
let mut offset: off_t = 0;
934-
let len = round_up(min_len, page_size()) as size_t;
929+
let mut addr: *u8 = ptr::null();
930+
let mut prot = 0;
931+
let mut flags = libc::MAP_PRIVATE;
932+
let mut fd = -1;
933+
let mut offset = 0;
934+
let len = round_up(min_len, page_size());
935935

936936
for &o in options.iter() {
937937
match o {
@@ -952,7 +952,7 @@ impl MemoryMap {
952952
if fd == -1 { flags |= libc::MAP_ANON; }
953953

954954
let r = unsafe {
955-
libc::mmap(addr, len, prot, flags, fd, offset)
955+
libc::mmap(addr as *c_void, len as size_t, prot, flags, fd, offset)
956956
};
957957
if r.equiv(&libc::MAP_FAILED) {
958958
Err(match errno() as c_int {
@@ -961,7 +961,7 @@ impl MemoryMap {
961961
libc::EINVAL => ErrUnaligned,
962962
libc::ENODEV => ErrNoMapSupport,
963963
libc::ENOMEM => ErrNoMem,
964-
code => ErrUnknown(code)
964+
code => ErrUnknown(code as int)
965965
})
966966
} else {
967967
Ok(MemoryMap {
@@ -987,7 +987,7 @@ impl Drop for MemoryMap {
987987
/// Unmap the mapping. Fails the task if `munmap` fails.
988988
fn drop(&mut self) {
989989
unsafe {
990-
match libc::munmap(self.data as *c_void, self.len) {
990+
match libc::munmap(self.data as *c_void, self.len as libc::size_t) {
991991
0 => (),
992992
-1 => match errno() as c_int {
993993
libc::EINVAL => error!("invalid addr or len"),
@@ -1011,7 +1011,7 @@ impl MemoryMap {
10111011
let mut executable = false;
10121012
let mut fd: c_int = -1;
10131013
let mut offset: uint = 0;
1014-
let len = round_up(min_len, page_size()) as SIZE_T;
1014+
let len = round_up(min_len, page_size());
10151015

10161016
for &o in options.iter() {
10171017
match o {
@@ -1040,7 +1040,7 @@ impl MemoryMap {
10401040
}
10411041
let r = unsafe {
10421042
libc::VirtualAlloc(lpAddress,
1043-
len,
1043+
len as SIZE_T,
10441044
libc::MEM_COMMIT | libc::MEM_RESERVE,
10451045
flProtect)
10461046
};
@@ -1085,7 +1085,7 @@ impl MemoryMap {
10851085
_ => Ok(MemoryMap {
10861086
data: r as *mut u8,
10871087
len: len,
1088-
kind: MapFile(mapping as *c_void)
1088+
kind: MapFile(mapping as *u8)
10891089
})
10901090
}
10911091
}
@@ -1116,7 +1116,7 @@ impl Drop for MemoryMap {
11161116
match self.kind {
11171117
MapVirtual => {
11181118
if libc::VirtualFree(self.data as *mut c_void,
1119-
self.len,
1119+
self.len as size_t,
11201120
libc::MEM_RELEASE) == FALSE {
11211121
error!("VirtualFree failed: {}", errno());
11221122
}

src/libstd/rc.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ impl<T> Drop for Rc<T> {
7878
if (*self.ptr).strong == 0 {
7979
read_ptr(self.borrow()); // destroy the contained object
8080
if (*self.ptr).weak == 0 {
81-
exchange_free(self.ptr as *mut u8 as *i8)
81+
exchange_free(self.ptr as *u8)
8282
}
8383
}
8484
}
@@ -153,7 +153,7 @@ impl<T> Drop for Weak<T> {
153153
if self.ptr != 0 as *mut RcBox<T> {
154154
(*self.ptr).weak -= 1;
155155
if (*self.ptr).weak == 0 && (*self.ptr).strong == 0 {
156-
exchange_free(self.ptr as *mut u8 as *i8)
156+
exchange_free(self.ptr as *u8)
157157
}
158158
}
159159
}

src/libstd/reflect.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ Runtime type reflection
1717
#[allow(missing_doc)];
1818

1919
use unstable::intrinsics::{Disr, Opaque, TyDesc, TyVisitor};
20-
use libc::c_void;
2120
use mem;
2221
use unstable::raw;
2322

@@ -28,7 +27,7 @@ use unstable::raw;
2827
* then build a MovePtrAdaptor wrapped around your struct.
2928
*/
3029
pub trait MovePtr {
31-
fn move_ptr(&mut self, adjustment: |*c_void| -> *c_void);
30+
fn move_ptr(&mut self, adjustment: |*u8| -> *u8);
3231
fn push_ptr(&mut self);
3332
fn pop_ptr(&mut self);
3433
}
@@ -50,12 +49,12 @@ pub fn MovePtrAdaptor<V:TyVisitor + MovePtr>(v: V) -> MovePtrAdaptor<V> {
5049
impl<V:TyVisitor + MovePtr> MovePtrAdaptor<V> {
5150
#[inline]
5251
pub fn bump(&mut self, sz: uint) {
53-
self.inner.move_ptr(|p| ((p as uint) + sz) as *c_void)
52+
self.inner.move_ptr(|p| ((p as uint) + sz) as *u8)
5453
}
5554

5655
#[inline]
5756
pub fn align(&mut self, a: uint) {
58-
self.inner.move_ptr(|p| align(p as uint, a) as *c_void)
57+
self.inner.move_ptr(|p| align(p as uint, a) as *u8)
5958
}
6059

6160
#[inline]

0 commit comments

Comments
 (0)