Skip to content

Commit faaba3e

Browse files
committed
Handle fallout in os
os::env(), os::args(), and related functions now use Vec<T> instead of ~[T].
1 parent 7272ea5 commit faaba3e

File tree

2 files changed

+45
-41
lines changed

2 files changed

+45
-41
lines changed

src/libstd/os.rs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ fn with_env_lock<T>(f: || -> T) -> T {
167167
///
168168
/// Invalid UTF-8 bytes are replaced with \uFFFD. See `str::from_utf8_lossy()`
169169
/// for details.
170-
pub fn env() -> ~[(~str,~str)] {
170+
pub fn env() -> Vec<(~str,~str)> {
171171
env_as_bytes().move_iter().map(|(k,v)| {
172172
let k = str::from_utf8_lossy(k).into_owned();
173173
let v = str::from_utf8_lossy(v).into_owned();
@@ -177,7 +177,7 @@ pub fn env() -> ~[(~str,~str)] {
177177

178178
/// Returns a vector of (variable, value) byte-vector pairs for all the
179179
/// environment variables of the current process.
180-
pub fn env_as_bytes() -> ~[(~[u8],~[u8])] {
180+
pub fn env_as_bytes() -> Vec<(~[u8],~[u8])> {
181181
unsafe {
182182
#[cfg(windows)]
183183
unsafe fn get_env_pairs() -> Vec<~[u8]> {
@@ -223,16 +223,16 @@ pub fn env_as_bytes() -> ~[(~[u8],~[u8])] {
223223
fn env_convert(input: Vec<~[u8]>) -> Vec<(~[u8], ~[u8])> {
224224
let mut pairs = Vec::new();
225225
for p in input.iter() {
226-
let vs: ~[&[u8]] = p.splitn(1, |b| *b == '=' as u8).collect();
227-
let key = vs[0].to_owned();
228-
let val = if vs.len() < 2 { box [] } else { vs[1].to_owned() };
226+
let mut it = p.splitn(1, |b| *b == '=' as u8);
227+
let key = it.next().unwrap().to_owned();
228+
let val = it.next().unwrap_or(&[]).to_owned();
229229
pairs.push((key, val));
230230
}
231231
pairs
232232
}
233233
with_env_lock(|| {
234234
let unparsed_environ = get_env_pairs();
235-
env_convert(unparsed_environ).move_iter().collect()
235+
env_convert(unparsed_environ)
236236
})
237237
}
238238
}
@@ -415,7 +415,7 @@ pub fn dll_filename(base: &str) -> ~str {
415415
pub fn self_exe_name() -> Option<Path> {
416416

417417
#[cfg(target_os = "freebsd")]
418-
fn load_self() -> Option<~[u8]> {
418+
fn load_self() -> Option<Vec<u8>> {
419419
unsafe {
420420
use libc::funcs::bsd44::*;
421421
use libc::consts::os::extra::*;
@@ -435,23 +435,23 @@ pub fn self_exe_name() -> Option<Path> {
435435
if err != 0 { return None; }
436436
if sz == 0 { return None; }
437437
v.set_len(sz as uint - 1); // chop off trailing NUL
438-
Some(v.move_iter().collect())
438+
Some(v)
439439
}
440440
}
441441

442442
#[cfg(target_os = "linux")]
443443
#[cfg(target_os = "android")]
444-
fn load_self() -> Option<~[u8]> {
444+
fn load_self() -> Option<Vec<u8>> {
445445
use std::io;
446446

447447
match io::fs::readlink(&Path::new("/proc/self/exe")) {
448-
Ok(path) => Some(path.as_vec().to_owned()),
448+
Ok(path) => Some(path.into_vec()),
449449
Err(..) => None
450450
}
451451
}
452452

453453
#[cfg(target_os = "macos")]
454-
fn load_self() -> Option<~[u8]> {
454+
fn load_self() -> Option<Vec<u8>> {
455455
unsafe {
456456
use libc::funcs::extra::_NSGetExecutablePath;
457457
let mut sz: u32 = 0;
@@ -461,19 +461,19 @@ pub fn self_exe_name() -> Option<Path> {
461461
let err = _NSGetExecutablePath(v.as_mut_ptr() as *mut i8, &mut sz);
462462
if err != 0 { return None; }
463463
v.set_len(sz as uint - 1); // chop off trailing NUL
464-
Some(v.move_iter().collect())
464+
Some(v)
465465
}
466466
}
467467

468468
#[cfg(windows)]
469-
fn load_self() -> Option<~[u8]> {
469+
fn load_self() -> Option<Vec<u8>> {
470470
use str::OwnedStr;
471471

472472
unsafe {
473473
use os::win32::fill_utf16_buf_and_decode;
474474
fill_utf16_buf_and_decode(|buf, sz| {
475475
libc::GetModuleFileNameW(0u as libc::DWORD, buf, sz)
476-
}).map(|s| s.into_bytes())
476+
}).map(|s| s.into_strbuf().into_bytes())
477477
}
478478
}
479479

@@ -788,12 +788,12 @@ pub fn get_exit_status() -> int {
788788
}
789789

790790
#[cfg(target_os = "macos")]
791-
unsafe fn load_argc_and_argv(argc: int, argv: **c_char) -> ~[~[u8]] {
791+
unsafe fn load_argc_and_argv(argc: int, argv: **c_char) -> Vec<~[u8]> {
792792
use c_str::CString;
793793

794794
Vec::from_fn(argc as uint, |i| {
795795
CString::new(*argv.offset(i as int), false).as_bytes_no_nul().to_owned()
796-
}).move_iter().collect()
796+
})
797797
}
798798

799799
/**
@@ -802,7 +802,7 @@ unsafe fn load_argc_and_argv(argc: int, argv: **c_char) -> ~[~[u8]] {
802802
* Returns a list of the command line arguments.
803803
*/
804804
#[cfg(target_os = "macos")]
805-
fn real_args_as_bytes() -> ~[~[u8]] {
805+
fn real_args_as_bytes() -> Vec<~[u8]> {
806806
unsafe {
807807
let (argc, argv) = (*_NSGetArgc() as int,
808808
*_NSGetArgv() as **c_char);
@@ -813,7 +813,7 @@ fn real_args_as_bytes() -> ~[~[u8]] {
813813
#[cfg(target_os = "linux")]
814814
#[cfg(target_os = "android")]
815815
#[cfg(target_os = "freebsd")]
816-
fn real_args_as_bytes() -> ~[~[u8]] {
816+
fn real_args_as_bytes() -> Vec<~[u8]> {
817817
use rt;
818818

819819
match rt::args::clone() {
@@ -823,12 +823,12 @@ fn real_args_as_bytes() -> ~[~[u8]] {
823823
}
824824

825825
#[cfg(not(windows))]
826-
fn real_args() -> ~[~str] {
826+
fn real_args() -> Vec<~str> {
827827
real_args_as_bytes().move_iter().map(|v| str::from_utf8_lossy(v).into_owned()).collect()
828828
}
829829

830830
#[cfg(windows)]
831-
fn real_args() -> ~[~str] {
831+
fn real_args() -> Vec<~str> {
832832
use slice;
833833

834834
let mut nArgs: c_int = 0;
@@ -853,11 +853,11 @@ fn real_args() -> ~[~str] {
853853
LocalFree(szArgList as *c_void);
854854
}
855855

856-
return args.move_iter().collect();
856+
return args
857857
}
858858

859859
#[cfg(windows)]
860-
fn real_args_as_bytes() -> ~[~[u8]] {
860+
fn real_args_as_bytes() -> Vec<~[u8]> {
861861
real_args().move_iter().map(|s| s.into_bytes()).collect()
862862
}
863863

@@ -881,13 +881,13 @@ extern "system" {
881881
///
882882
/// The arguments are interpreted as utf-8, with invalid bytes replaced with \uFFFD.
883883
/// See `str::from_utf8_lossy` for details.
884-
pub fn args() -> ~[~str] {
884+
pub fn args() -> Vec<~str> {
885885
real_args()
886886
}
887887

888888
/// Returns the arguments which this program was started with (normally passed
889889
/// via the command line) as byte vectors.
890-
pub fn args_as_bytes() -> ~[~[u8]] {
890+
pub fn args_as_bytes() -> Vec<~[u8]> {
891891
real_args_as_bytes()
892892
}
893893

src/libstd/rt/args.rs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@
2121
//! FIXME #7756: This has a lot of C glue for lack of globals.
2222
2323
use option::Option;
24+
#[cfg(not(test))] use vec::Vec;
2425
#[cfg(test)] use option::{Some, None};
2526
#[cfg(test)] use realstd;
2627
#[cfg(test)] use realargs = realstd::rt::args;
28+
#[cfg(test)] use realstd::vec::Vec;
2729

2830
/// One-time global initialization.
2931
#[cfg(not(test))]
@@ -36,8 +38,8 @@ pub unsafe fn init(argc: int, argv: **u8) { realargs::init(argc, argv) }
3638
#[cfg(test)] pub unsafe fn cleanup() { realargs::cleanup() }
3739

3840
/// Take the global arguments from global storage.
39-
#[cfg(not(test))] pub fn take() -> Option<~[~[u8]]> { imp::take() }
40-
#[cfg(test)] pub fn take() -> Option<~[~[u8]]> {
41+
#[cfg(not(test))] pub fn take() -> Option<Vec<~[u8]>> { imp::take() }
42+
#[cfg(test)] pub fn take() -> Option<Vec<~[u8]>> {
4143
match realargs::take() {
4244
realstd::option::Some(a) => Some(a),
4345
realstd::option::None => None,
@@ -47,12 +49,12 @@ pub unsafe fn init(argc: int, argv: **u8) { realargs::init(argc, argv) }
4749
/// Give the global arguments to global storage.
4850
///
4951
/// It is an error if the arguments already exist.
50-
#[cfg(not(test))] pub fn put(args: ~[~[u8]]) { imp::put(args) }
51-
#[cfg(test)] pub fn put(args: ~[~[u8]]) { realargs::put(args) }
52+
#[cfg(not(test))] pub fn put(args: Vec<~[u8]>) { imp::put(args) }
53+
#[cfg(test)] pub fn put(args: Vec<~[u8]>) { realargs::put(args) }
5254

5355
/// Make a clone of the global arguments.
54-
#[cfg(not(test))] pub fn clone() -> Option<~[~[u8]]> { imp::clone() }
55-
#[cfg(test)] pub fn clone() -> Option<~[~[u8]]> {
56+
#[cfg(not(test))] pub fn clone() -> Option<Vec<~[u8]>> { imp::clone() }
57+
#[cfg(test)] pub fn clone() -> Option<Vec<~[u8]>> {
5658
match realargs::clone() {
5759
realstd::option::Some(a) => Some(a),
5860
realstd::option::None => None,
@@ -69,6 +71,7 @@ mod imp {
6971
use iter::Iterator;
7072
use unstable::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT};
7173
use mem;
74+
use vec::Vec;
7275
#[cfg(not(test))] use str::StrSlice;
7376
#[cfg(not(test))] use ptr::RawPtr;
7477

@@ -87,26 +90,26 @@ mod imp {
8790
lock.destroy();
8891
}
8992

90-
pub fn take() -> Option<~[~[u8]]> {
93+
pub fn take() -> Option<Vec<~[u8]>> {
9194
with_lock(|| unsafe {
9295
let ptr = get_global_ptr();
9396
let val = mem::replace(&mut *ptr, None);
94-
val.as_ref().map(|s: &~~[~[u8]]| (**s).clone())
97+
val.as_ref().map(|s: &~Vec<~[u8]>| (**s).clone())
9598
})
9699
}
97100

98-
pub fn put(args: ~[~[u8]]) {
101+
pub fn put(args: Vec<~[u8]>) {
99102
with_lock(|| unsafe {
100103
let ptr = get_global_ptr();
101104
rtassert!((*ptr).is_none());
102105
(*ptr) = Some(box args.clone());
103106
})
104107
}
105108

106-
pub fn clone() -> Option<~[~[u8]]> {
109+
pub fn clone() -> Option<Vec<~[u8]>> {
107110
with_lock(|| unsafe {
108111
let ptr = get_global_ptr();
109-
(*ptr).as_ref().map(|s: &~~[~[u8]]| (**s).clone())
112+
(*ptr).as_ref().map(|s: &~Vec<~[u8]>| (**s).clone())
110113
})
111114
}
112115

@@ -117,13 +120,13 @@ mod imp {
117120
}
118121
}
119122

120-
fn get_global_ptr() -> *mut Option<~~[~[u8]]> {
123+
fn get_global_ptr() -> *mut Option<~Vec<~[u8]>> {
121124
unsafe { cast::transmute(&global_args_ptr) }
122125
}
123126

124127
// Copied from `os`.
125128
#[cfg(not(test))]
126-
unsafe fn load_argc_and_argv(argc: int, argv: **u8) -> ~[~[u8]] {
129+
unsafe fn load_argc_and_argv(argc: int, argv: **u8) -> Vec<~[u8]> {
127130
use c_str::CString;
128131
use ptr::RawPtr;
129132
use libc;
@@ -133,7 +136,7 @@ mod imp {
133136
Vec::from_fn(argc as uint, |i| {
134137
let cs = CString::new(*(argv as **libc::c_char).offset(i as int), false);
135138
cs.as_bytes_no_nul().to_owned()
136-
}).move_iter().collect()
139+
})
137140
}
138141

139142
#[cfg(test)]
@@ -170,22 +173,23 @@ mod imp {
170173
#[cfg(target_os = "win32", not(test))]
171174
mod imp {
172175
use option::Option;
176+
use vec::Vec;
173177

174178
pub unsafe fn init(_argc: int, _argv: **u8) {
175179
}
176180

177181
pub fn cleanup() {
178182
}
179183

180-
pub fn take() -> Option<~[~[u8]]> {
184+
pub fn take() -> Option<Vec<~[u8]>> {
181185
fail!()
182186
}
183187

184-
pub fn put(_args: ~[~[u8]]) {
188+
pub fn put(_args: Vec<~[u8]>) {
185189
fail!()
186190
}
187191

188-
pub fn clone() -> Option<~[~[u8]]> {
192+
pub fn clone() -> Option<Vec<~[u8]>> {
189193
fail!()
190194
}
191195
}

0 commit comments

Comments
 (0)