Skip to content

Replace some uses of deprecated os (mostly os::args) functions #22402

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/libstd/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,12 +488,20 @@ impl Iterator for Args {
fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
}

impl ExactSizeIterator for Args {
fn len(&self) -> usize { self.inner.len() }
}

impl Iterator for ArgsOs {
type Item = OsString;
fn next(&mut self) -> Option<OsString> { self.inner.next() }
fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
}

impl ExactSizeIterator for ArgsOs {
fn len(&self) -> usize { self.inner.len() }
}

/// Returns the page size of the current architecture in bytes.
pub fn page_size() -> usize {
os_imp::page_size()
Expand Down
4 changes: 4 additions & 0 deletions src/libstd/sys/unix/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,10 @@ impl Iterator for Args {
fn size_hint(&self) -> (usize, Option<usize>) { self.iter.size_hint() }
}

impl ExactSizeIterator for Args {
fn len(&self) -> usize { self.iter.len() }
}

/// Returns the command line arguments
///
/// Returns a list of the command line arguments.
Expand Down
8 changes: 6 additions & 2 deletions src/libstd/sys/windows/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use os::windows::*;
use error::Error as StdError;
use ffi::{OsString, OsStr, AsOsStr};
use fmt;
use iter::Range;
use ops::Range;
use libc::types::os::arch::extra::LPWCH;
use libc::{self, c_int, c_void};
use mem;
Expand Down Expand Up @@ -303,6 +303,10 @@ impl Iterator for Args {
fn size_hint(&self) -> (usize, Option<usize>) { self.range.size_hint() }
}

impl ExactSizeIterator for Args {
fn len(&self) -> usize { self.range.len() }
}

impl Drop for Args {
fn drop(&mut self) {
unsafe { c::LocalFree(self.cur as *mut c_void); }
Expand All @@ -315,7 +319,7 @@ pub fn args() -> Args {
let lpCmdLine = c::GetCommandLineW();
let szArgList = c::CommandLineToArgvW(lpCmdLine, &mut nArgs);

Args { cur: szArgList, range: range(0, nArgs as isize) }
Args { cur: szArgList, range: 0..(nArgs as isize) }
}
}

Expand Down
13 changes: 7 additions & 6 deletions src/rustbook/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
//! Implementation of the `build` subcommand, used to compile a book.

use std::os;
use std::env;
use std::old_io;
use std::old_io::{fs, File, BufferedWriter, TempDir, IoResult};

Expand Down Expand Up @@ -80,10 +81,10 @@ fn render(book: &Book, tgt: &Path) -> CliResult<()> {
let out_path = tgt.join(item.path.dirname());

let src;
if os::args().len() < 3 {
if env::args().len() < 3 {
src = os::getcwd().unwrap().clone();
} else {
src = Path::new(os::args()[2].clone());
src = Path::new(env::args().nth(2).unwrap().clone());
}
// preprocess the markdown, rerouting markdown references to html references
let markdown_data = try!(File::open(&src.join(&item.path)).read_to_string());
Expand Down Expand Up @@ -153,16 +154,16 @@ impl Subcommand for Build {
let src;
let tgt;

if os::args().len() < 3 {
if env::args().len() < 3 {
src = cwd.clone();
} else {
src = Path::new(os::args()[2].clone());
src = Path::new(env::args().nth(2).unwrap().clone());
}

if os::args().len() < 4 {
if env::args().len() < 4 {
tgt = cwd.join("_book");
} else {
tgt = Path::new(os::args()[3].clone());
tgt = Path::new(env::args().nth(3).unwrap().clone());
}

try!(fs::mkdir(&tgt, old_io::USER_DIR));
Expand Down
5 changes: 3 additions & 2 deletions src/rustbook/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
#![feature(core)]
#![feature(io)]
#![feature(os)]
#![feature(env)]
#![feature(path)]
#![feature(rustdoc)]

extern crate rustdoc;

use std::os;
use std::env;
use subcommand::Subcommand;
use term::Term;

Expand Down Expand Up @@ -48,7 +49,7 @@ mod javascript;
#[cfg(not(test))] // thanks #12327
fn main() {
let mut term = Term::new();
let cmd = os::args();
let cmd: Vec<_> = env::args().collect();

if cmd.len() < 1 {
help::usage()
Expand Down
4 changes: 2 additions & 2 deletions src/rustbook/term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
//! An abstraction of the terminal. Eventually, provide color and
//! verbosity support. For now, just a wrapper around stdout/stderr.

use std::os;
use std::env;
use std::old_io::stdio;

pub struct Term {
Expand All @@ -28,6 +28,6 @@ impl Term {
pub fn err(&mut self, msg: &str) {
// swallow any errors
let _ = self.err.write_line(msg);
os::set_exit_status(101);
env::set_exit_status(101);
}
}
57 changes: 28 additions & 29 deletions src/test/bench/core-map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#![feature(unboxed_closures)]

use std::collections::{BTreeMap, HashMap, HashSet};
use std::os;
use std::env;
use std::rand::{Rng, IsaacRng, SeedableRng};
use std::time::Duration;

Expand All @@ -20,33 +20,33 @@ fn timed<F>(label: &str, f: F) where F: FnMut() {
}

trait MutableMap {
fn insert(&mut self, k: uint, v: uint);
fn remove(&mut self, k: &uint) -> bool;
fn find(&self, k: &uint) -> Option<&uint>;
fn insert(&mut self, k: usize, v: usize);
fn remove(&mut self, k: &usize) -> bool;
fn find(&self, k: &usize) -> Option<&usize>;
}

impl MutableMap for BTreeMap<uint, uint> {
fn insert(&mut self, k: uint, v: uint) { self.insert(k, v); }
fn remove(&mut self, k: &uint) -> bool { self.remove(k).is_some() }
fn find(&self, k: &uint) -> Option<&uint> { self.get(k) }
impl MutableMap for BTreeMap<usize, usize> {
fn insert(&mut self, k: usize, v: usize) { self.insert(k, v); }
fn remove(&mut self, k: &usize) -> bool { self.remove(k).is_some() }
fn find(&self, k: &usize) -> Option<&usize> { self.get(k) }
}
impl MutableMap for HashMap<uint, uint> {
fn insert(&mut self, k: uint, v: uint) { self.insert(k, v); }
fn remove(&mut self, k: &uint) -> bool { self.remove(k).is_some() }
fn find(&self, k: &uint) -> Option<&uint> { self.get(k) }
impl MutableMap for HashMap<usize, usize> {
fn insert(&mut self, k: usize, v: usize) { self.insert(k, v); }
fn remove(&mut self, k: &usize) -> bool { self.remove(k).is_some() }
fn find(&self, k: &usize) -> Option<&usize> { self.get(k) }
}

fn ascending<M: MutableMap>(map: &mut M, n_keys: uint) {
fn ascending<M: MutableMap>(map: &mut M, n_keys: usize) {
println!(" Ascending integers:");

timed("insert", || {
for i in 0u..n_keys {
for i in 0..n_keys {
map.insert(i, i + 1);
}
});

timed("search", || {
for i in 0u..n_keys {
for i in 0..n_keys {
assert_eq!(map.find(&i).unwrap(), &(i + 1));
}
});
Expand All @@ -58,7 +58,7 @@ fn ascending<M: MutableMap>(map: &mut M, n_keys: uint) {
});
}

fn descending<M: MutableMap>(map: &mut M, n_keys: uint) {
fn descending<M: MutableMap>(map: &mut M, n_keys: usize) {
println!(" Descending integers:");

timed("insert", || {
Expand All @@ -80,32 +80,31 @@ fn descending<M: MutableMap>(map: &mut M, n_keys: uint) {
});
}

fn vector<M: MutableMap>(map: &mut M, n_keys: uint, dist: &[uint]) {
fn vector<M: MutableMap>(map: &mut M, n_keys: usize, dist: &[usize]) {
timed("insert", || {
for i in 0u..n_keys {
for i in 0..n_keys {
map.insert(dist[i], i + 1);
}
});

timed("search", || {
for i in 0u..n_keys {
for i in 0..n_keys {
assert_eq!(map.find(&dist[i]).unwrap(), &(i + 1));
}
});

timed("remove", || {
for i in 0u..n_keys {
for i in 0..n_keys {
assert!(map.remove(&dist[i]));
}
});
}

fn main() {
let args = os::args();
let args = args;
let mut args = env::args();
let n_keys = {
if args.len() == 2 {
args[1].parse::<uint>().unwrap()
args.nth(1).unwrap().parse::<usize>().unwrap()
} else {
1000000
}
Expand All @@ -131,37 +130,37 @@ fn main() {
println!("{}", "\nBTreeMap:");

{
let mut map: BTreeMap<uint,uint> = BTreeMap::new();
let mut map: BTreeMap<usize,usize> = BTreeMap::new();
ascending(&mut map, n_keys);
}

{
let mut map: BTreeMap<uint,uint> = BTreeMap::new();
let mut map: BTreeMap<usize,usize> = BTreeMap::new();
descending(&mut map, n_keys);
}

{
println!(" Random integers:");
let mut map: BTreeMap<uint,uint> = BTreeMap::new();
let mut map: BTreeMap<usize,usize> = BTreeMap::new();
vector(&mut map, n_keys, &rand);
}

// FIXME: #9970
println!("{}", "\nHashMap:");

{
let mut map: HashMap<uint,uint> = HashMap::new();
let mut map: HashMap<usize,usize> = HashMap::new();
ascending(&mut map, n_keys);
}

{
let mut map: HashMap<uint,uint> = HashMap::new();
let mut map: HashMap<usize,usize> = HashMap::new();
descending(&mut map, n_keys);
}

{
println!(" Random integers:");
let mut map: HashMap<uint,uint> = HashMap::new();
let mut map: HashMap<usize,usize> = HashMap::new();
vector(&mut map, n_keys, &rand);
}
}
Loading