From: Nick Cameron Date: Mon, 23 Nov 2015 23:11:20 +0000 (+1300) Subject: rustfmt: libflate, libfmt_macros, libgetopts, libgraphviz, liblog, librand X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=5fb6531903c4e425f48a859463bee20cfcc7f7c5;p=rust.git rustfmt: libflate, libfmt_macros, libgetopts, libgraphviz, liblog, librand --- diff --git a/src/libflate/lib.rs b/src/libflate/lib.rs index 513379b6558..c5f0800e71f 100644 --- a/src/libflate/lib.rs +++ b/src/libflate/lib.rs @@ -82,7 +82,7 @@ fn drop(&mut self) { } #[link(name = "miniz", kind = "static")] -extern { +extern "C" { /// Raw miniz compression function. fn tdefl_compress_mem_to_heap(psrc_buf: *const c_void, src_buf_len: size_t, diff --git a/src/libfmt_macros/lib.rs b/src/libfmt_macros/lib.rs index dc69f38e4e7..2cd046dc38a 100644 --- a/src/libfmt_macros/lib.rs +++ b/src/libfmt_macros/lib.rs @@ -187,7 +187,7 @@ pub fn new(s: &'a str) -> Parser<'a> { Parser { input: s, cur: s.char_indices().peekable(), - errors: vec!(), + errors: vec![], } } @@ -236,7 +236,7 @@ fn ws(&mut self) { if c.is_whitespace() { self.cur.next(); } else { - break + break; } } } @@ -274,9 +274,7 @@ fn position(&mut self) -> Position<'a> { ArgumentIs(i) } else { match self.cur.peek() { - Some(&(_, c)) if c.is_alphabetic() => { - ArgumentNamed(self.word()) - } + Some(&(_, c)) if c.is_alphabetic() => ArgumentNamed(self.word()), _ => ArgumentNext, } } @@ -294,7 +292,7 @@ fn format(&mut self) -> FormatSpec<'a> { ty: &self.input[..0], }; if !self.consume(':') { - return spec + return spec; } // fill character @@ -419,7 +417,7 @@ fn integer(&mut self) -> Option { found = true; self.cur.next(); } else { - break + break; } } if found { @@ -447,7 +445,7 @@ fn fmtdflt() -> FormatSpec<'static> { precision: CountImplied, width: CountImplied, ty: "", - } + }; } fn musterr(s: &str) { diff --git a/src/libgetopts/lib.rs b/src/libgetopts/lib.rs index f49451f827d..c744121f843 100644 --- a/src/libgetopts/lib.rs +++ b/src/libgetopts/lib.rs @@ -98,7 +98,9 @@ #![feature(str_char)] #![cfg_attr(test, feature(rustc_private))] -#[cfg(test)] #[macro_use] extern crate log; +#[cfg(test)] +#[macro_use] +extern crate log; use self::Name::*; use self::HasArg::*; @@ -174,7 +176,7 @@ pub struct OptGroup { /// Whether option has an argument pub hasarg: HasArg, /// How often it can occur - pub occur: Occur + pub occur: Occur, } /// Describes whether an option is given at all or has a value. @@ -239,7 +241,7 @@ fn from_str(nm: &str) -> Name { fn to_string(&self) -> String { match *self { Short(ch) => ch.to_string(), - Long(ref s) => s.to_owned() + Long(ref s) => s.to_owned(), } } } @@ -257,33 +259,37 @@ pub fn long_to_short(&self) -> Opt { } = (*self).clone(); match (short_name.len(), long_name.len()) { - (0,0) => panic!("this long-format option was given no name"), - (0,_) => Opt { - name: Long((long_name)), - hasarg: hasarg, - occur: occur, - aliases: Vec::new() - }, - (1,0) => Opt { - name: Short(short_name.char_at(0)), - hasarg: hasarg, - occur: occur, - aliases: Vec::new() - }, - (1,_) => Opt { - name: Long((long_name)), - hasarg: hasarg, - occur: occur, - aliases: vec!( - Opt { - name: Short(short_name.char_at(0)), - hasarg: hasarg, - occur: occur, - aliases: Vec::new() - } - ) - }, - (_,_) => panic!("something is wrong with the long-form opt") + (0, 0) => panic!("this long-format option was given no name"), + (0, _) => { + Opt { + name: Long((long_name)), + hasarg: hasarg, + occur: occur, + aliases: Vec::new(), + } + } + (1, 0) => { + Opt { + name: Short(short_name.char_at(0)), + hasarg: hasarg, + occur: occur, + aliases: Vec::new(), + } + } + (1, _) => { + Opt { + name: Long((long_name)), + hasarg: hasarg, + occur: occur, + aliases: vec![Opt { + name: Short(short_name.char_at(0)), + hasarg: hasarg, + occur: occur, + aliases: Vec::new(), + }], + } + } + (_, _) => panic!("something is wrong with the long-form opt"), } } } @@ -292,7 +298,7 @@ impl Matches { fn opt_vals(&self, nm: &str) -> Vec { match find_opt(&self.opts[..], Name::from_str(nm)) { Some(id) => self.vals[id].clone(), - None => panic!("No option '{}' defined", nm) + None => panic!("No option '{}' defined", nm), } } @@ -331,7 +337,7 @@ pub fn opts_str(&self, names: &[String]) -> Option { for nm in names { match self.opt_val(&nm[..]) { Some(Val(ref s)) => return Some(s.clone()), - _ => () + _ => (), } } None @@ -347,7 +353,7 @@ pub fn opt_strs(&self, nm: &str) -> Vec { for v in &r { match *v { Val(ref s) => acc.push((*s).clone()), - _ => () + _ => (), } } acc @@ -361,7 +367,7 @@ pub fn opt_str(&self, nm: &str) -> Option { } match vals[0] { Val(ref s) => Some((*s).clone()), - _ => None + _ => None, } } @@ -378,11 +384,10 @@ pub fn opt_default(&self, nm: &str, def: &str) -> Option { } else { match vals[0] { Val(ref s) => Some((*s).clone()), - _ => Some(def.to_owned()) + _ => Some(def.to_owned()), } } } - } fn is_arg(arg: &str) -> bool { @@ -393,7 +398,7 @@ fn find_opt(opts: &[Opt], nm: Name) -> Option { // Search main options. let pos = opts.iter().position(|opt| opt.name == nm); if pos.is_some() { - return pos + return pos; } // Search in aliases. @@ -422,7 +427,7 @@ pub fn reqopt(short_name: &str, long_name: &str, desc: &str, hint: &str) -> OptG hint: hint.to_owned(), desc: desc.to_owned(), hasarg: Yes, - occur: Req + occur: Req, } } @@ -442,7 +447,7 @@ pub fn optopt(short_name: &str, long_name: &str, desc: &str, hint: &str) -> OptG hint: hint.to_owned(), desc: desc.to_owned(), hasarg: Yes, - occur: Optional + occur: Optional, } } @@ -460,7 +465,7 @@ pub fn optflag(short_name: &str, long_name: &str, desc: &str) -> OptGroup { hint: "".to_owned(), desc: desc.to_owned(), hasarg: No, - occur: Optional + occur: Optional, } } @@ -479,7 +484,7 @@ pub fn optflagmulti(short_name: &str, long_name: &str, desc: &str) -> OptGroup { hint: "".to_owned(), desc: desc.to_owned(), hasarg: No, - occur: Multi + occur: Multi, } } @@ -499,7 +504,7 @@ pub fn optflagopt(short_name: &str, long_name: &str, desc: &str, hint: &str) -> hint: hint.to_owned(), desc: desc.to_owned(), hasarg: Maybe, - occur: Optional + occur: Optional, } } @@ -520,7 +525,7 @@ pub fn optmulti(short_name: &str, long_name: &str, desc: &str, hint: &str) -> Op hint: hint.to_owned(), desc: desc.to_owned(), hasarg: Yes, - occur: Multi + occur: Multi, } } @@ -530,7 +535,8 @@ pub fn opt(short_name: &str, desc: &str, hint: &str, hasarg: HasArg, - occur: Occur) -> OptGroup { + occur: Occur) + -> OptGroup { let len = short_name.len(); assert!(len == 1 || len == 0); OptGroup { @@ -539,28 +545,18 @@ pub fn opt(short_name: &str, hint: hint.to_owned(), desc: desc.to_owned(), hasarg: hasarg, - occur: occur + occur: occur, } } impl fmt::Display for Fail { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { - ArgumentMissing(ref nm) => { - write!(f, "Argument to option '{}' missing.", *nm) - } - UnrecognizedOption(ref nm) => { - write!(f, "Unrecognized option: '{}'.", *nm) - } - OptionMissing(ref nm) => { - write!(f, "Required option '{}' missing.", *nm) - } - OptionDuplicated(ref nm) => { - write!(f, "Option '{}' given more than once.", *nm) - } - UnexpectedArgument(ref nm) => { - write!(f, "Option '{}' does not take an argument.", *nm) - } + ArgumentMissing(ref nm) => write!(f, "Argument to option '{}' missing.", *nm), + UnrecognizedOption(ref nm) => write!(f, "Unrecognized option: '{}'.", *nm), + OptionMissing(ref nm) => write!(f, "Required option '{}' missing.", *nm), + OptionDuplicated(ref nm) => write!(f, "Option '{}' given more than once.", *nm), + UnexpectedArgument(ref nm) => write!(f, "Option '{}' does not take an argument.", *nm), } } } @@ -577,7 +573,9 @@ pub fn getopts(args: &[String], optgrps: &[OptGroup]) -> Result { let opts: Vec = optgrps.iter().map(|x| x.long_to_short()).collect(); let n_opts = opts.len(); - fn f(_x: usize) -> Vec { Vec::new() } + fn f(_x: usize) -> Vec { + Vec::new() + } let mut vals: Vec<_> = (0..n_opts).map(f).collect(); let mut free: Vec = Vec::new(); @@ -590,7 +588,10 @@ fn f(_x: usize) -> Vec { Vec::new() } free.push(cur); } else if cur == "--" { let mut j = i + 1; - while j < l { free.push(args[j].clone()); j += 1; } + while j < l { + free.push(args[j].clone()); + j += 1; + } break; } else { let mut names; @@ -599,10 +600,9 @@ fn f(_x: usize) -> Vec { Vec::new() } let tail = &cur[2..curlen]; let tail_eq: Vec<&str> = tail.split('=').collect(); if tail_eq.len() <= 1 { - names = vec!(Long(tail.to_owned())); + names = vec![Long(tail.to_owned())]; } else { - names = - vec!(Long(tail_eq[0].to_owned())); + names = vec![Long(tail_eq[0].to_owned())]; i_arg = Some(tail_eq[1].to_owned()); } } else { @@ -612,23 +612,22 @@ fn f(_x: usize) -> Vec { Vec::new() } let ch = cur.char_at(j); let opt = Short(ch); - /* In a series of potential options (eg. -aheJ), if we - see one which takes an argument, we assume all - subsequent characters make up the argument. This - allows options such as -L/usr/local/lib/foo to be - interpreted correctly - */ + // In a series of potential options (eg. -aheJ), if we + // see one which takes an argument, we assume all + // subsequent characters make up the argument. This + // allows options such as -L/usr/local/lib/foo to be + // interpreted correctly let opt_id = match find_opt(&opts, opt.clone()) { - Some(id) => id, - None => return Err(UnrecognizedOption(opt.to_string())) + Some(id) => id, + None => return Err(UnrecognizedOption(opt.to_string())), }; names.push(opt); let arg_follows = match opts[opt_id].hasarg { Yes | Maybe => true, - No => false + No => false, }; let next = j + ch.len_utf8(); @@ -644,44 +643,42 @@ fn f(_x: usize) -> Vec { Vec::new() } for nm in &names { name_pos += 1; let optid = match find_opt(&opts, (*nm).clone()) { - Some(id) => id, - None => return Err(UnrecognizedOption(nm.to_string())) + Some(id) => id, + None => return Err(UnrecognizedOption(nm.to_string())), }; match opts[optid].hasarg { - No => { - if name_pos == names.len() && !i_arg.is_none() { - return Err(UnexpectedArgument(nm.to_string())); - } - let v = &mut vals[optid]; - v.push(Given); - } - Maybe => { - if !i_arg.is_none() { - let v = &mut vals[optid]; - v.push(Val((i_arg.clone()) - .unwrap())); - } else if name_pos < names.len() || i + 1 == l || - is_arg(&args[i + 1][..]) { + No => { + if name_pos == names.len() && !i_arg.is_none() { + return Err(UnexpectedArgument(nm.to_string())); + } let v = &mut vals[optid]; v.push(Given); - } else { - i += 1; - let v = &mut vals[optid]; - v.push(Val(args[i].clone())); } - } - Yes => { - if !i_arg.is_none() { - let v = &mut vals[optid]; - v.push(Val(i_arg.clone().unwrap())); - } else if i + 1 == l { - return Err(ArgumentMissing(nm.to_string())); - } else { - i += 1; - let v = &mut vals[optid]; - v.push(Val(args[i].clone())); + Maybe => { + if !i_arg.is_none() { + let v = &mut vals[optid]; + v.push(Val((i_arg.clone()).unwrap())); + } else if name_pos < names.len() || i + 1 == l || is_arg(&args[i + 1][..]) { + let v = &mut vals[optid]; + v.push(Given); + } else { + i += 1; + let v = &mut vals[optid]; + v.push(Val(args[i].clone())); + } + } + Yes => { + if !i_arg.is_none() { + let v = &mut vals[optid]; + v.push(Val(i_arg.clone().unwrap())); + } else if i + 1 == l { + return Err(ArgumentMissing(nm.to_string())); + } else { + i += 1; + let v = &mut vals[optid]; + v.push(Val(args[i].clone())); + } } - } } } } @@ -700,7 +697,7 @@ fn f(_x: usize) -> Vec { Vec::new() } Ok(Matches { opts: opts, vals: vals, - free: free + free: free, }) } @@ -783,7 +780,8 @@ pub fn usage(brief: &str, opts: &[OptGroup]) -> String { row }); - format!("{}\n\nOptions:\n{}\n", brief, + format!("{}\n\nOptions:\n{}\n", + brief, rows.collect::>().join("\n")) } @@ -836,19 +834,19 @@ pub fn short_usage(program_name: &str, opts: &[OptGroup]) -> String { #[derive(Copy, Clone)] enum SplitWithinState { - A, // leading whitespace, initial state - B, // words - C, // internal and trailing whitespace + A, // leading whitespace, initial state + B, // words + C, // internal and trailing whitespace } #[derive(Copy, Clone)] enum Whitespace { Ws, // current char is whitespace - Cr // current char is not whitespace + Cr, // current char is not whitespace } #[derive(Copy, Clone)] enum LengthLimit { UnderLim, // current char makes current substring still fit in limit - OverLim // current char makes current substring no longer fit in limit + OverLim, // current char makes current substring no longer fit in limit } @@ -863,8 +861,8 @@ enum LengthLimit { /// /// Panics during iteration if the string contains a non-whitespace /// sequence longer than the limit. -fn each_split_within(ss: &str, lim: usize, mut it: F) -> bool where - F: FnMut(&str) -> bool +fn each_split_within(ss: &str, lim: usize, mut it: F) -> bool + where F: FnMut(&str) -> bool { // Just for fun, let's write this as a state machine: @@ -883,18 +881,31 @@ fn each_split_within(ss: &str, lim: usize, mut it: F) -> bool where } let mut machine = |cont: &mut bool, (i, c): (usize, char)| -> bool { - let whitespace = if c.is_whitespace() { Ws } else { Cr }; - let limit = if (i - slice_start + 1) <= lim { UnderLim } else { OverLim }; + let whitespace = if c.is_whitespace() { + Ws + } else { + Cr + }; + let limit = if (i - slice_start + 1) <= lim { + UnderLim + } else { + OverLim + }; state = match (state, whitespace, limit) { - (A, Ws, _) => { A } - (A, Cr, _) => { slice_start = i; last_start = i; B } - - (B, Cr, UnderLim) => { B } - (B, Cr, OverLim) if (i - last_start + 1) > lim - => panic!("word starting with {} longer than limit!", - &ss[last_start..i + 1]), - (B, Cr, OverLim) => { + (A, Ws, _) => A, + (A, Cr, _) => { + slice_start = i; + last_start = i; + B + } + + (B, Cr, UnderLim) => B, + (B, Cr, OverLim) if (i - last_start + 1) > lim => { + panic!("word starting with {} longer than limit!", + &ss[last_start..i + 1]) + } + (B, Cr, OverLim) => { *cont = it(&ss[slice_start..last_end]); slice_start = last_start; B @@ -903,7 +914,7 @@ fn each_split_within(ss: &str, lim: usize, mut it: F) -> bool where last_end = i; C } - (B, Ws, OverLim) => { + (B, Ws, OverLim) => { last_end = i; *cont = it(&ss[slice_start..last_end]); A @@ -913,20 +924,18 @@ fn each_split_within(ss: &str, lim: usize, mut it: F) -> bool where last_start = i; B } - (C, Cr, OverLim) => { + (C, Cr, OverLim) => { *cont = it(&ss[slice_start..last_end]); slice_start = i; last_start = i; last_end = i; B } - (C, Ws, OverLim) => { + (C, Ws, OverLim) => { *cont = it(&ss[slice_start..last_end]); A } - (C, Ws, UnderLim) => { - C - } + (C, Ws, UnderLim) => C, }; *cont @@ -935,7 +944,11 @@ fn each_split_within(ss: &str, lim: usize, mut it: F) -> bool where ss.char_indices().all(|x| machine(&mut cont, x)); // Let the automaton 'run out' by supplying trailing whitespace - while cont && match state { B | C => true, A => false } { + while cont && + match state { + B | C => true, + A => false, + } { machine(&mut cont, (fake_i, ' ')); fake_i += 1; } @@ -946,19 +959,21 @@ fn each_split_within(ss: &str, lim: usize, mut it: F) -> bool where fn test_split_within() { fn t(s: &str, i: usize, u: &[String]) { let mut v = Vec::new(); - each_split_within(s, i, |s| { v.push(s.to_string()); true }); - assert!(v.iter().zip(u).all(|(a,b)| a == b)); + each_split_within(s, i, |s| { + v.push(s.to_string()); + true + }); + assert!(v.iter().zip(u).all(|(a, b)| a == b)); } t("", 0, &[]); t("", 15, &[]); t("hello", 15, &["hello".to_string()]); - t("\nMary had a little lamb\nLittle lamb\n", 15, &[ - "Mary had a".to_string(), - "little lamb".to_string(), - "Little lamb".to_string() - ]); - t("\nMary had a little lamb\nLittle lamb\n", ::std::usize::MAX, - &["Mary had a little lamb\nLittle lamb".to_string()]); + t("\nMary had a little lamb\nLittle lamb\n", + 15, + &["Mary had a".to_string(), "little lamb".to_string(), "Little lamb".to_string()]); + t("\nMary had a little lamb\nLittle lamb\n", + ::std::usize::MAX, + &["Mary had a little lamb\nLittle lamb".to_string()]); } #[cfg(test)] @@ -972,442 +987,446 @@ mod tests { // Tests for reqopt #[test] fn test_reqopt() { - let long_args = vec!("--test=20".to_string()); - let opts = vec!(reqopt("t", "test", "testing", "TEST")); + let long_args = vec!["--test=20".to_string()]; + let opts = vec![reqopt("t", "test", "testing", "TEST")]; let rs = getopts(&long_args, &opts); match rs { - Ok(ref m) => { - assert!(m.opt_present("test")); - assert_eq!(m.opt_str("test").unwrap(), "20"); - assert!(m.opt_present("t")); - assert_eq!(m.opt_str("t").unwrap(), "20"); - } - _ => { panic!("test_reqopt failed (long arg)"); } + Ok(ref m) => { + assert!(m.opt_present("test")); + assert_eq!(m.opt_str("test").unwrap(), "20"); + assert!(m.opt_present("t")); + assert_eq!(m.opt_str("t").unwrap(), "20"); + } + _ => { + panic!("test_reqopt failed (long arg)"); + } } - let short_args = vec!("-t".to_string(), "20".to_string()); + let short_args = vec!["-t".to_string(), "20".to_string()]; match getopts(&short_args, &opts) { - Ok(ref m) => { - assert!((m.opt_present("test"))); - assert_eq!(m.opt_str("test").unwrap(), "20"); - assert!((m.opt_present("t"))); - assert_eq!(m.opt_str("t").unwrap(), "20"); - } - _ => { panic!("test_reqopt failed (short arg)"); } + Ok(ref m) => { + assert!((m.opt_present("test"))); + assert_eq!(m.opt_str("test").unwrap(), "20"); + assert!((m.opt_present("t"))); + assert_eq!(m.opt_str("t").unwrap(), "20"); + } + _ => { + panic!("test_reqopt failed (short arg)"); + } } } #[test] fn test_reqopt_missing() { - let args = vec!("blah".to_string()); - let opts = vec!(reqopt("t", "test", "testing", "TEST")); + let args = vec!["blah".to_string()]; + let opts = vec![reqopt("t", "test", "testing", "TEST")]; let rs = getopts(&args, &opts); match rs { - Err(OptionMissing(_)) => {}, - _ => panic!() + Err(OptionMissing(_)) => {} + _ => panic!(), } } #[test] fn test_reqopt_no_arg() { - let long_args = vec!("--test".to_string()); - let opts = vec!(reqopt("t", "test", "testing", "TEST")); + let long_args = vec!["--test".to_string()]; + let opts = vec![reqopt("t", "test", "testing", "TEST")]; let rs = getopts(&long_args, &opts); match rs { - Err(ArgumentMissing(_)) => {}, - _ => panic!() + Err(ArgumentMissing(_)) => {} + _ => panic!(), } - let short_args = vec!("-t".to_string()); + let short_args = vec!["-t".to_string()]; match getopts(&short_args, &opts) { - Err(ArgumentMissing(_)) => {}, - _ => panic!() + Err(ArgumentMissing(_)) => {} + _ => panic!(), } } #[test] fn test_reqopt_multi() { - let args = vec!("--test=20".to_string(), "-t".to_string(), "30".to_string()); - let opts = vec!(reqopt("t", "test", "testing", "TEST")); + let args = vec!["--test=20".to_string(), "-t".to_string(), "30".to_string()]; + let opts = vec![reqopt("t", "test", "testing", "TEST")]; let rs = getopts(&args, &opts); match rs { - Err(OptionDuplicated(_)) => {}, - _ => panic!() + Err(OptionDuplicated(_)) => {} + _ => panic!(), } } // Tests for optopt #[test] fn test_optopt() { - let long_args = vec!("--test=20".to_string()); - let opts = vec!(optopt("t", "test", "testing", "TEST")); + let long_args = vec!["--test=20".to_string()]; + let opts = vec![optopt("t", "test", "testing", "TEST")]; let rs = getopts(&long_args, &opts); match rs { - Ok(ref m) => { - assert!(m.opt_present("test")); - assert_eq!(m.opt_str("test").unwrap(), "20"); - assert!((m.opt_present("t"))); - assert_eq!(m.opt_str("t").unwrap(), "20"); - } - _ => panic!() + Ok(ref m) => { + assert!(m.opt_present("test")); + assert_eq!(m.opt_str("test").unwrap(), "20"); + assert!((m.opt_present("t"))); + assert_eq!(m.opt_str("t").unwrap(), "20"); + } + _ => panic!(), } - let short_args = vec!("-t".to_string(), "20".to_string()); + let short_args = vec!["-t".to_string(), "20".to_string()]; match getopts(&short_args, &opts) { - Ok(ref m) => { - assert!((m.opt_present("test"))); - assert_eq!(m.opt_str("test").unwrap(), "20"); - assert!((m.opt_present("t"))); - assert_eq!(m.opt_str("t").unwrap(), "20"); - } - _ => panic!() + Ok(ref m) => { + assert!((m.opt_present("test"))); + assert_eq!(m.opt_str("test").unwrap(), "20"); + assert!((m.opt_present("t"))); + assert_eq!(m.opt_str("t").unwrap(), "20"); + } + _ => panic!(), } } #[test] fn test_optopt_missing() { - let args = vec!("blah".to_string()); - let opts = vec!(optopt("t", "test", "testing", "TEST")); + let args = vec!["blah".to_string()]; + let opts = vec![optopt("t", "test", "testing", "TEST")]; let rs = getopts(&args, &opts); match rs { - Ok(ref m) => { - assert!(!m.opt_present("test")); - assert!(!m.opt_present("t")); - } - _ => panic!() + Ok(ref m) => { + assert!(!m.opt_present("test")); + assert!(!m.opt_present("t")); + } + _ => panic!(), } } #[test] fn test_optopt_no_arg() { - let long_args = vec!("--test".to_string()); - let opts = vec!(optopt("t", "test", "testing", "TEST")); + let long_args = vec!["--test".to_string()]; + let opts = vec![optopt("t", "test", "testing", "TEST")]; let rs = getopts(&long_args, &opts); match rs { - Err(ArgumentMissing(_)) => {}, - _ => panic!() + Err(ArgumentMissing(_)) => {} + _ => panic!(), } - let short_args = vec!("-t".to_string()); + let short_args = vec!["-t".to_string()]; match getopts(&short_args, &opts) { - Err(ArgumentMissing(_)) => {}, - _ => panic!() + Err(ArgumentMissing(_)) => {} + _ => panic!(), } } #[test] fn test_optopt_multi() { - let args = vec!("--test=20".to_string(), "-t".to_string(), "30".to_string()); - let opts = vec!(optopt("t", "test", "testing", "TEST")); + let args = vec!["--test=20".to_string(), "-t".to_string(), "30".to_string()]; + let opts = vec![optopt("t", "test", "testing", "TEST")]; let rs = getopts(&args, &opts); match rs { - Err(OptionDuplicated(_)) => {}, - _ => panic!() + Err(OptionDuplicated(_)) => {} + _ => panic!(), } } // Tests for optflag #[test] fn test_optflag() { - let long_args = vec!("--test".to_string()); - let opts = vec!(optflag("t", "test", "testing")); + let long_args = vec!["--test".to_string()]; + let opts = vec![optflag("t", "test", "testing")]; let rs = getopts(&long_args, &opts); match rs { - Ok(ref m) => { - assert!(m.opt_present("test")); - assert!(m.opt_present("t")); - } - _ => panic!() + Ok(ref m) => { + assert!(m.opt_present("test")); + assert!(m.opt_present("t")); + } + _ => panic!(), } - let short_args = vec!("-t".to_string()); + let short_args = vec!["-t".to_string()]; match getopts(&short_args, &opts) { - Ok(ref m) => { - assert!(m.opt_present("test")); - assert!(m.opt_present("t")); - } - _ => panic!() + Ok(ref m) => { + assert!(m.opt_present("test")); + assert!(m.opt_present("t")); + } + _ => panic!(), } } #[test] fn test_optflag_missing() { - let args = vec!("blah".to_string()); - let opts = vec!(optflag("t", "test", "testing")); + let args = vec!["blah".to_string()]; + let opts = vec![optflag("t", "test", "testing")]; let rs = getopts(&args, &opts); match rs { - Ok(ref m) => { - assert!(!m.opt_present("test")); - assert!(!m.opt_present("t")); - } - _ => panic!() + Ok(ref m) => { + assert!(!m.opt_present("test")); + assert!(!m.opt_present("t")); + } + _ => panic!(), } } #[test] fn test_optflag_long_arg() { - let args = vec!("--test=20".to_string()); - let opts = vec!(optflag("t", "test", "testing")); + let args = vec!["--test=20".to_string()]; + let opts = vec![optflag("t", "test", "testing")]; let rs = getopts(&args, &opts); match rs { - Err(UnexpectedArgument(_)) => {}, - _ => panic!() + Err(UnexpectedArgument(_)) => {} + _ => panic!(), } } #[test] fn test_optflag_multi() { - let args = vec!("--test".to_string(), "-t".to_string()); - let opts = vec!(optflag("t", "test", "testing")); + let args = vec!["--test".to_string(), "-t".to_string()]; + let opts = vec![optflag("t", "test", "testing")]; let rs = getopts(&args, &opts); match rs { - Err(OptionDuplicated(_)) => {}, - _ => panic!() + Err(OptionDuplicated(_)) => {} + _ => panic!(), } } #[test] fn test_optflag_short_arg() { - let args = vec!("-t".to_string(), "20".to_string()); - let opts = vec!(optflag("t", "test", "testing")); + let args = vec!["-t".to_string(), "20".to_string()]; + let opts = vec![optflag("t", "test", "testing")]; let rs = getopts(&args, &opts); match rs { - Ok(ref m) => { - // The next variable after the flag is just a free argument + Ok(ref m) => { + // The next variable after the flag is just a free argument - assert!(m.free[0] == "20"); - } - _ => panic!() + assert!(m.free[0] == "20"); + } + _ => panic!(), } } // Tests for optflagmulti #[test] fn test_optflagmulti_short1() { - let args = vec!("-v".to_string()); - let opts = vec!(optflagmulti("v", "verbose", "verbosity")); + let args = vec!["-v".to_string()]; + let opts = vec![optflagmulti("v", "verbose", "verbosity")]; let rs = getopts(&args, &opts); match rs { - Ok(ref m) => { - assert_eq!(m.opt_count("v"), 1); - } - _ => panic!() + Ok(ref m) => { + assert_eq!(m.opt_count("v"), 1); + } + _ => panic!(), } } #[test] fn test_optflagmulti_short2a() { - let args = vec!("-v".to_string(), "-v".to_string()); - let opts = vec!(optflagmulti("v", "verbose", "verbosity")); + let args = vec!["-v".to_string(), "-v".to_string()]; + let opts = vec![optflagmulti("v", "verbose", "verbosity")]; let rs = getopts(&args, &opts); match rs { - Ok(ref m) => { - assert_eq!(m.opt_count("v"), 2); - } - _ => panic!() + Ok(ref m) => { + assert_eq!(m.opt_count("v"), 2); + } + _ => panic!(), } } #[test] fn test_optflagmulti_short2b() { - let args = vec!("-vv".to_string()); - let opts = vec!(optflagmulti("v", "verbose", "verbosity")); + let args = vec!["-vv".to_string()]; + let opts = vec![optflagmulti("v", "verbose", "verbosity")]; let rs = getopts(&args, &opts); match rs { - Ok(ref m) => { - assert_eq!(m.opt_count("v"), 2); - } - _ => panic!() + Ok(ref m) => { + assert_eq!(m.opt_count("v"), 2); + } + _ => panic!(), } } #[test] fn test_optflagmulti_long1() { - let args = vec!("--verbose".to_string()); - let opts = vec!(optflagmulti("v", "verbose", "verbosity")); + let args = vec!["--verbose".to_string()]; + let opts = vec![optflagmulti("v", "verbose", "verbosity")]; let rs = getopts(&args, &opts); match rs { - Ok(ref m) => { - assert_eq!(m.opt_count("verbose"), 1); - } - _ => panic!() + Ok(ref m) => { + assert_eq!(m.opt_count("verbose"), 1); + } + _ => panic!(), } } #[test] fn test_optflagmulti_long2() { - let args = vec!("--verbose".to_string(), "--verbose".to_string()); - let opts = vec!(optflagmulti("v", "verbose", "verbosity")); + let args = vec!["--verbose".to_string(), "--verbose".to_string()]; + let opts = vec![optflagmulti("v", "verbose", "verbosity")]; let rs = getopts(&args, &opts); match rs { - Ok(ref m) => { - assert_eq!(m.opt_count("verbose"), 2); - } - _ => panic!() + Ok(ref m) => { + assert_eq!(m.opt_count("verbose"), 2); + } + _ => panic!(), } } #[test] fn test_optflagmulti_mix() { - let args = vec!("--verbose".to_string(), "-v".to_string(), - "-vv".to_string(), "verbose".to_string()); - let opts = vec!(optflagmulti("v", "verbose", "verbosity")); + let args = vec!["--verbose".to_string(), + "-v".to_string(), + "-vv".to_string(), + "verbose".to_string()]; + let opts = vec![optflagmulti("v", "verbose", "verbosity")]; let rs = getopts(&args, &opts); match rs { - Ok(ref m) => { - assert_eq!(m.opt_count("verbose"), 4); - assert_eq!(m.opt_count("v"), 4); - } - _ => panic!() + Ok(ref m) => { + assert_eq!(m.opt_count("verbose"), 4); + assert_eq!(m.opt_count("v"), 4); + } + _ => panic!(), } } // Tests for optmulti #[test] fn test_optmulti() { - let long_args = vec!("--test=20".to_string()); - let opts = vec!(optmulti("t", "test", "testing", "TEST")); + let long_args = vec!["--test=20".to_string()]; + let opts = vec![optmulti("t", "test", "testing", "TEST")]; let rs = getopts(&long_args, &opts); match rs { - Ok(ref m) => { - assert!((m.opt_present("test"))); - assert_eq!(m.opt_str("test").unwrap(), "20"); - assert!((m.opt_present("t"))); - assert_eq!(m.opt_str("t").unwrap(), "20"); - } - _ => panic!() + Ok(ref m) => { + assert!((m.opt_present("test"))); + assert_eq!(m.opt_str("test").unwrap(), "20"); + assert!((m.opt_present("t"))); + assert_eq!(m.opt_str("t").unwrap(), "20"); + } + _ => panic!(), } - let short_args = vec!("-t".to_string(), "20".to_string()); + let short_args = vec!["-t".to_string(), "20".to_string()]; match getopts(&short_args, &opts) { - Ok(ref m) => { - assert!((m.opt_present("test"))); - assert_eq!(m.opt_str("test").unwrap(), "20"); - assert!((m.opt_present("t"))); - assert_eq!(m.opt_str("t").unwrap(), "20"); - } - _ => panic!() + Ok(ref m) => { + assert!((m.opt_present("test"))); + assert_eq!(m.opt_str("test").unwrap(), "20"); + assert!((m.opt_present("t"))); + assert_eq!(m.opt_str("t").unwrap(), "20"); + } + _ => panic!(), } } #[test] fn test_optmulti_missing() { - let args = vec!("blah".to_string()); - let opts = vec!(optmulti("t", "test", "testing", "TEST")); + let args = vec!["blah".to_string()]; + let opts = vec![optmulti("t", "test", "testing", "TEST")]; let rs = getopts(&args, &opts); match rs { - Ok(ref m) => { - assert!(!m.opt_present("test")); - assert!(!m.opt_present("t")); - } - _ => panic!() + Ok(ref m) => { + assert!(!m.opt_present("test")); + assert!(!m.opt_present("t")); + } + _ => panic!(), } } #[test] fn test_optmulti_no_arg() { - let long_args = vec!("--test".to_string()); - let opts = vec!(optmulti("t", "test", "testing", "TEST")); + let long_args = vec!["--test".to_string()]; + let opts = vec![optmulti("t", "test", "testing", "TEST")]; let rs = getopts(&long_args, &opts); match rs { - Err(ArgumentMissing(_)) => {}, - _ => panic!() + Err(ArgumentMissing(_)) => {} + _ => panic!(), } - let short_args = vec!("-t".to_string()); + let short_args = vec!["-t".to_string()]; match getopts(&short_args, &opts) { - Err(ArgumentMissing(_)) => {}, - _ => panic!() + Err(ArgumentMissing(_)) => {} + _ => panic!(), } } #[test] fn test_optmulti_multi() { - let args = vec!("--test=20".to_string(), "-t".to_string(), "30".to_string()); - let opts = vec!(optmulti("t", "test", "testing", "TEST")); + let args = vec!["--test=20".to_string(), "-t".to_string(), "30".to_string()]; + let opts = vec![optmulti("t", "test", "testing", "TEST")]; let rs = getopts(&args, &opts); match rs { - Ok(ref m) => { - assert!(m.opt_present("test")); - assert_eq!(m.opt_str("test").unwrap(), "20"); - assert!(m.opt_present("t")); - assert_eq!(m.opt_str("t").unwrap(), "20"); - let pair = m.opt_strs("test"); - assert!(pair[0] == "20"); - assert!(pair[1] == "30"); - } - _ => panic!() + Ok(ref m) => { + assert!(m.opt_present("test")); + assert_eq!(m.opt_str("test").unwrap(), "20"); + assert!(m.opt_present("t")); + assert_eq!(m.opt_str("t").unwrap(), "20"); + let pair = m.opt_strs("test"); + assert!(pair[0] == "20"); + assert!(pair[1] == "30"); + } + _ => panic!(), } } #[test] fn test_unrecognized_option() { - let long_args = vec!("--untest".to_string()); - let opts = vec!(optmulti("t", "test", "testing", "TEST")); + let long_args = vec!["--untest".to_string()]; + let opts = vec![optmulti("t", "test", "testing", "TEST")]; let rs = getopts(&long_args, &opts); match rs { - Err(UnrecognizedOption(_)) => {}, - _ => panic!() + Err(UnrecognizedOption(_)) => {} + _ => panic!(), } - let short_args = vec!("-u".to_string()); + let short_args = vec!["-u".to_string()]; match getopts(&short_args, &opts) { - Err(UnrecognizedOption(_)) => {}, - _ => panic!() + Err(UnrecognizedOption(_)) => {} + _ => panic!(), } } #[test] fn test_combined() { - let args = - vec!("prog".to_string(), - "free1".to_string(), - "-s".to_string(), - "20".to_string(), - "free2".to_string(), - "--flag".to_string(), - "--long=30".to_string(), - "-f".to_string(), - "-m".to_string(), - "40".to_string(), - "-m".to_string(), - "50".to_string(), - "-n".to_string(), - "-A B".to_string(), - "-n".to_string(), - "-60 70".to_string()); - let opts = - vec!(optopt("s", "something", "something", "SOMETHING"), - optflag("", "flag", "a flag"), - reqopt("", "long", "hi", "LONG"), - optflag("f", "", "another flag"), - optmulti("m", "", "mmmmmm", "YUM"), - optmulti("n", "", "nothing", "NOTHING"), - optopt("", "notpresent", "nothing to see here", "NOPE")); + let args = vec!["prog".to_string(), + "free1".to_string(), + "-s".to_string(), + "20".to_string(), + "free2".to_string(), + "--flag".to_string(), + "--long=30".to_string(), + "-f".to_string(), + "-m".to_string(), + "40".to_string(), + "-m".to_string(), + "50".to_string(), + "-n".to_string(), + "-A B".to_string(), + "-n".to_string(), + "-60 70".to_string()]; + let opts = vec![optopt("s", "something", "something", "SOMETHING"), + optflag("", "flag", "a flag"), + reqopt("", "long", "hi", "LONG"), + optflag("f", "", "another flag"), + optmulti("m", "", "mmmmmm", "YUM"), + optmulti("n", "", "nothing", "NOTHING"), + optopt("", "notpresent", "nothing to see here", "NOPE")]; let rs = getopts(&args, &opts); match rs { - Ok(ref m) => { - assert!(m.free[0] == "prog"); - assert!(m.free[1] == "free1"); - assert_eq!(m.opt_str("s").unwrap(), "20"); - assert!(m.free[2] == "free2"); - assert!((m.opt_present("flag"))); - assert_eq!(m.opt_str("long").unwrap(), "30"); - assert!((m.opt_present("f"))); - let pair = m.opt_strs("m"); - assert!(pair[0] == "40"); - assert!(pair[1] == "50"); - let pair = m.opt_strs("n"); - assert!(pair[0] == "-A B"); - assert!(pair[1] == "-60 70"); - assert!((!m.opt_present("notpresent"))); - } - _ => panic!() + Ok(ref m) => { + assert!(m.free[0] == "prog"); + assert!(m.free[1] == "free1"); + assert_eq!(m.opt_str("s").unwrap(), "20"); + assert!(m.free[2] == "free2"); + assert!((m.opt_present("flag"))); + assert_eq!(m.opt_str("long").unwrap(), "30"); + assert!((m.opt_present("f"))); + let pair = m.opt_strs("m"); + assert!(pair[0] == "40"); + assert!(pair[1] == "50"); + let pair = m.opt_strs("n"); + assert!(pair[0] == "-A B"); + assert!(pair[1] == "-60 70"); + assert!((!m.opt_present("notpresent"))); + } + _ => panic!(), } } #[test] fn test_multi() { - let opts = vec!(optopt("e", "", "encrypt", "ENCRYPT"), - optopt("", "encrypt", "encrypt", "ENCRYPT"), - optopt("f", "", "flag", "FLAG")); + let opts = vec![optopt("e", "", "encrypt", "ENCRYPT"), + optopt("", "encrypt", "encrypt", "ENCRYPT"), + optopt("f", "", "flag", "FLAG")]; - let args_single = vec!("-e".to_string(), "foo".to_string()); + let args_single = vec!["-e".to_string(), "foo".to_string()]; let matches_single = &match getopts(&args_single, &opts) { - result::Result::Ok(m) => m, - result::Result::Err(_) => panic!() + result::Result::Ok(m) => m, + result::Result::Err(_) => panic!(), }; assert!(matches_single.opts_present(&["e".to_string()])); assert!(matches_single.opts_present(&["encrypt".to_string(), "e".to_string()])); @@ -1422,11 +1441,13 @@ fn test_multi() { assert_eq!(matches_single.opts_str(&["encrypt".to_string(), "e".to_string()]).unwrap(), "foo"); - let args_both = vec!("-e".to_string(), "foo".to_string(), "--encrypt".to_string(), - "foo".to_string()); + let args_both = vec!["-e".to_string(), + "foo".to_string(), + "--encrypt".to_string(), + "foo".to_string()]; let matches_both = &match getopts(&args_both, &opts) { - result::Result::Ok(m) => m, - result::Result::Err(_) => panic!() + result::Result::Ok(m) => m, + result::Result::Err(_) => panic!(), }; assert!(matches_both.opts_present(&["e".to_string()])); assert!(matches_both.opts_present(&["encrypt".to_string()])); @@ -1437,7 +1458,8 @@ fn test_multi() { assert!(!matches_both.opts_present(&[])); assert_eq!(matches_both.opts_str(&["e".to_string()]).unwrap(), "foo"); - assert_eq!(matches_both.opts_str(&["encrypt".to_string()]).unwrap(), "foo"); + assert_eq!(matches_both.opts_str(&["encrypt".to_string()]).unwrap(), + "foo"); assert_eq!(matches_both.opts_str(&["e".to_string(), "encrypt".to_string()]).unwrap(), "foo"); assert_eq!(matches_both.opts_str(&["encrypt".to_string(), "e".to_string()]).unwrap(), @@ -1446,12 +1468,12 @@ fn test_multi() { #[test] fn test_nospace() { - let args = vec!("-Lfoo".to_string(), "-M.".to_string()); - let opts = vec!(optmulti("L", "", "library directory", "LIB"), - optmulti("M", "", "something", "MMMM")); + let args = vec!["-Lfoo".to_string(), "-M.".to_string()]; + let opts = vec![optmulti("L", "", "library directory", "LIB"), + optmulti("M", "", "something", "MMMM")]; let matches = &match getopts(&args, &opts) { - result::Result::Ok(m) => m, - result::Result::Err(_) => panic!() + result::Result::Ok(m) => m, + result::Result::Err(_) => panic!(), }; assert!(matches.opts_present(&["L".to_string()])); assert_eq!(matches.opts_str(&["L".to_string()]).unwrap(), "foo"); @@ -1462,12 +1484,12 @@ fn test_nospace() { #[test] fn test_nospace_conflict() { - let args = vec!("-vvLverbose".to_string(), "-v".to_string() ); - let opts = vec!(optmulti("L", "", "library directory", "LIB"), - optflagmulti("v", "verbose", "Verbose")); + let args = vec!["-vvLverbose".to_string(), "-v".to_string()]; + let opts = vec![optmulti("L", "", "library directory", "LIB"), + optflagmulti("v", "verbose", "Verbose")]; let matches = &match getopts(&args, &opts) { - result::Result::Ok(m) => m, - result::Result::Err(e) => panic!( "{}", e ) + result::Result::Ok(m) => m, + result::Result::Err(e) => panic!("{}", e), }; assert!(matches.opts_present(&["L".to_string()])); assert_eq!(matches.opts_str(&["L".to_string()]).unwrap(), "verbose"); @@ -1483,10 +1505,12 @@ fn test_long_to_short() { occur: Occur::Req, aliases: Vec::new(), }; - short.aliases = vec!(Opt { name: Name::Short('b'), - hasarg: HasArg::Yes, - occur: Occur::Req, - aliases: Vec::new() }); + short.aliases = vec![Opt { + name: Name::Short('b'), + hasarg: HasArg::Yes, + occur: Occur::Req, + aliases: Vec::new(), + }]; let verbose = reqopt("b", "banana", "some bananas", "VAL"); assert!(verbose.long_to_short() == short); @@ -1494,10 +1518,9 @@ fn test_long_to_short() { #[test] fn test_aliases_long_and_short() { - let opts = vec!( - optflagmulti("a", "apple", "Desc")); + let opts = vec![optflagmulti("a", "apple", "Desc")]; - let args = vec!("-a".to_string(), "--apple".to_string(), "-a".to_string()); + let args = vec!["-a".to_string(), "--apple".to_string(), "-a".to_string()]; let matches = getopts(&args, &opts).unwrap(); assert_eq!(3, matches.opt_count("a")); @@ -1506,13 +1529,11 @@ fn test_aliases_long_and_short() { #[test] fn test_usage() { - let optgroups = vec!( - reqopt("b", "banana", "Desc", "VAL"), - optopt("a", "012345678901234567890123456789", - "Desc", "VAL"), - optflag("k", "kiwi", "Desc"), - optflagopt("p", "", "Desc", "VAL"), - optmulti("l", "", "Desc", "VAL")); + let optgroups = vec![reqopt("b", "banana", "Desc", "VAL"), + optopt("a", "012345678901234567890123456789", "Desc", "VAL"), + optflag("k", "kiwi", "Desc"), + optflagopt("p", "", "Desc", "VAL"), + optmulti("l", "", "Desc", "VAL")]; let expected = "Usage: fruits @@ -1538,11 +1559,13 @@ fn test_usage_description_wrapping() { // indentation should be 24 spaces // lines wrap after 78: or rather descriptions wrap after 54 - let optgroups = vec!( - optflag("k", "kiwi", - "This is a long description which won't be wrapped..+.."), // 54 - optflag("a", "apple", - "This is a long description which _will_ be wrapped..+..")); + let optgroups = vec![optflag("k", + "kiwi", + // 54 + "This is a long description which won't be wrapped..+.."), + optflag("a", + "apple", + "This is a long description which _will_ be wrapped..+..")]; let expected = "Usage: fruits @@ -1562,12 +1585,14 @@ fn test_usage_description_wrapping() { #[test] fn test_usage_description_multibyte_handling() { - let optgroups = vec!( - optflag("k", "k\u{2013}w\u{2013}", - "The word kiwi is normally spelled with two i's"), - optflag("a", "apple", - "This \u{201C}description\u{201D} has some characters that could \ -confuse the line wrapping; an apple costs 0.51€ in some parts of Europe.")); + let optgroups = vec![optflag("k", + "k\u{2013}w\u{2013}", + "The word kiwi is normally spelled with two i's"), + optflag("a", + "apple", + "This \u{201C}description\u{201D} has some characters that \ + could confuse the line wrapping; an apple costs 0.51€ in \ + some parts of Europe.")]; let expected = "Usage: fruits @@ -1588,13 +1613,11 @@ fn test_usage_description_multibyte_handling() { #[test] fn test_short_usage() { - let optgroups = vec!( - reqopt("b", "banana", "Desc", "VAL"), - optopt("a", "012345678901234567890123456789", - "Desc", "VAL"), - optflag("k", "kiwi", "Desc"), - optflagopt("p", "", "Desc", "VAL"), - optmulti("l", "", "Desc", "VAL")); + let optgroups = vec![reqopt("b", "banana", "Desc", "VAL"), + optopt("a", "012345678901234567890123456789", "Desc", "VAL"), + optflag("k", "kiwi", "Desc"), + optflagopt("p", "", "Desc", "VAL"), + optmulti("l", "", "Desc", "VAL")]; let expected = "Usage: fruits -b VAL [-a VAL] [-k] [-p [VAL]] [-l VAL]..".to_string(); let generated_usage = short_usage("fruits", &optgroups); diff --git a/src/libgraphviz/lib.rs b/src/libgraphviz/lib.rs index 1fb54ed9071..14a8d78c0b2 100644 --- a/src/libgraphviz/lib.rs +++ b/src/libgraphviz/lib.rs @@ -416,10 +416,10 @@ pub fn new>(name: Name) -> Result, ()> { _ => return Err(()), } if !chars.all(is_constituent) { - return Err(()) + return Err(()); } } - return Ok(Id{ name: name }); + return Ok(Id { name: name }); fn is_letter_or_underscore(c: char) -> bool { in_range('a', c, 'z') || in_range('A', c, 'Z') || c == '_' @@ -496,11 +496,10 @@ fn edge_style(&'a self, _e: &E) -> Style { /// Escape tags in such a way that it is suitable for inclusion in a /// Graphviz HTML label. pub fn escape_html(s: &str) -> String { - s - .replace("&", "&") - .replace("\"", """) - .replace("<", "<") - .replace(">", ">") + s.replace("&", "&") + .replace("\"", """) + .replace("<", "<") + .replace(">", ">") } impl<'a> LabelText<'a> { @@ -523,9 +522,11 @@ fn escape_char(c: char, mut f: F) // not escaping \\, since Graphviz escString needs to // interpret backslashes; see EscStr above. '\\' => f(c), - _ => for c in c.escape_default() { - f(c) - }, + _ => { + for c in c.escape_default() { + f(c) + } + } } } fn escape_str(s: &str) -> String { @@ -553,11 +554,13 @@ pub fn to_dot_string(&self) -> String { fn pre_escaped_content(self) -> Cow<'a, str> { match self { EscStr(s) => s, - LabelStr(s) => if s.contains('\\') { - (&*s).escape_default().into_cow() - } else { - s - }, + LabelStr(s) => { + if s.contains('\\') { + (&*s).escape_default().into_cow() + } else { + s + } + } HtmlStr(s) => s, } } @@ -738,7 +741,12 @@ struct Edge { } fn edge(from: usize, to: usize, label: &'static str, style: Style) -> Edge { - Edge { from: from, to: to, label: label, style: style } + Edge { + from: from, + to: to, + label: label, + style: style, + } } struct LabelledGraph { @@ -1009,7 +1017,7 @@ fn single_cyclic_node() { #[test] fn hasse_diagram() { - let labels = AllNodesLabelled(vec!("{x,y}", "{x}", "{y}", "{}")); + let labels = AllNodesLabelled(vec!["{x,y}", "{x}", "{y}", "{}"]); let r = test_input(LabelledGraph::new("hasse_diagram", labels, vec![edge(0, 1, "", Style::None), @@ -1033,7 +1041,7 @@ fn hasse_diagram() { #[test] fn left_aligned_text() { - let labels = AllNodesLabelled(vec!( + let labels = AllNodesLabelled(vec![ "if test {\ \\l branch1\ \\l} else {\ @@ -1043,7 +1051,7 @@ fn left_aligned_text() { \\l", "branch1", "branch2", - "afterward")); + "afterward"]); let mut writer = Vec::new(); diff --git a/src/liblog/directive.rs b/src/liblog/directive.rs index 3958969cfca..18163703e88 100644 --- a/src/liblog/directive.rs +++ b/src/liblog/directive.rs @@ -49,7 +49,7 @@ pub fn parse_logging_spec(spec: &str) -> (Vec, Option) { if let Some(m) = mods { for s in m.split(',') { if s.is_empty() { - continue + continue; } let mut parts = s.split('='); let (log_level, name) = match (parts.next(), @@ -69,13 +69,13 @@ pub fn parse_logging_spec(spec: &str) -> (Vec, Option) { Some(num) => (num, Some(part0)), _ => { println!("warning: invalid logging spec '{}', ignoring it", part1); - continue + continue; } } } _ => { println!("warning: invalid logging spec '{}', ignoring it", s); - continue + continue; } }; dirs.push(LogDirective { diff --git a/src/liblog/lib.rs b/src/liblog/lib.rs index 1de7ebfffde..850c5511dd5 100644 --- a/src/liblog/lib.rs +++ b/src/liblog/lib.rs @@ -296,7 +296,7 @@ pub fn log(level: u32, loc: &'static LogLocation, args: fmt::Arguments) { n => { let filter = mem::transmute::<_, &String>(n); if !args.to_string().contains(filter) { - return + return; } } } @@ -375,7 +375,7 @@ pub fn mod_enabled(level: u32, module: &str) -> bool { // check being expanded manually in the logging macro, this function checks // the log level again. if level > unsafe { LOG_LEVEL } { - return false + return false; } // This assertion should never get tripped unless we're in an at_exit @@ -393,9 +393,7 @@ fn enabled(level: u32, module: &str, iter: slice::Iter) for directive in iter.rev() { match directive.name { Some(ref name) if !module.starts_with(&name[..]) => {} - Some(..) | None => { - return level <= directive.level - } + Some(..) | None => return level <= directive.level, } } level <= DEFAULT_LOG_LEVEL diff --git a/src/librand/chacha.rs b/src/librand/chacha.rs index 411ca836080..e2c157f98a6 100644 --- a/src/librand/chacha.rs +++ b/src/librand/chacha.rs @@ -12,8 +12,8 @@ use {Rng, SeedableRng, Rand}; -const KEY_WORDS : usize = 8; // 8 words for the 256-bit key -const STATE_WORDS : usize = 16; +const KEY_WORDS: usize = 8; // 8 words for the 256-bit key +const STATE_WORDS: usize = 16; const CHACHA_ROUNDS: usize = 20; // Cryptographically secure from 8 upwards as of this writing /// A random number generator that uses the ChaCha20 algorithm [1]. @@ -77,7 +77,6 @@ macro_rules! double_round{ } impl ChaChaRng { - /// Create an ChaCha random number generator using the default /// fixed key of 8 zero words. pub fn new_unseeded() -> ChaChaRng { @@ -173,7 +172,6 @@ fn next_u32(&mut self) -> u32 { } impl<'a> SeedableRng<&'a [u32]> for ChaChaRng { - fn reseed(&mut self, seed: &'a [u32]) { // reset state self.init(&[0; KEY_WORDS]); diff --git a/src/librand/distributions/gamma.rs b/src/librand/distributions/gamma.rs index fceda64cbb3..8cd7ac06f99 100644 --- a/src/librand/distributions/gamma.rs +++ b/src/librand/distributions/gamma.rs @@ -89,7 +89,7 @@ pub fn new(shape: f64, scale: f64) -> Gamma { let repr = match shape { 1.0 => One(Exp::new(1.0 / scale)), - 0.0 ... 1.0 => Small(GammaSmallShape::new_raw(shape, scale)), + 0.0...1.0 => Small(GammaSmallShape::new_raw(shape, scale)), _ => Large(GammaLargeShape::new_raw(shape, scale)), }; Gamma { repr: repr } @@ -153,7 +153,8 @@ fn ind_sample(&self, rng: &mut R) -> f64 { loop { let StandardNormal(x) = rng.gen::(); let v_cbrt = 1.0 + self.c * x; - if v_cbrt <= 0.0 { // a^3 <= 0 iff a <= 0 + if v_cbrt <= 0.0 { + // a^3 <= 0 iff a <= 0 continue; } diff --git a/src/librand/distributions/mod.rs b/src/librand/distributions/mod.rs index 4c62e1a3504..a54c8df2352 100644 --- a/src/librand/distributions/mod.rs +++ b/src/librand/distributions/mod.rs @@ -118,8 +118,10 @@ pub fn new(items: &'a mut [Weighted]) -> WeightedChoice<'a, T> { for item in &mut *items { running_total = match running_total.checked_add(item.weight) { Some(n) => n, - None => panic!("WeightedChoice::new called with a total weight larger than a \ - usize can contain"), + None => { + panic!("WeightedChoice::new called with a total weight larger than a usize \ + can contain") + } }; item.weight = running_total; @@ -199,7 +201,6 @@ fn ind_sample(&self, rng: &mut R) -> T { /// * `pdf`: the probability density function /// * `zero_case`: manual sampling from the tail when we chose the /// bottom box (i.e. i == 0) - // the perf improvement (25-50%) is definitely worth the extra code // size from force-inlining. #[inline(always)] diff --git a/src/librand/isaac.rs b/src/librand/isaac.rs index ac2b9b07ffd..1f56a82eba8 100644 --- a/src/librand/isaac.rs +++ b/src/librand/isaac.rs @@ -54,7 +54,6 @@ pub struct IsaacRng { }; impl IsaacRng { - /// Create an ISAAC random number generator using the default /// fixed seed. pub fn new_unseeded() -> IsaacRng { @@ -403,7 +402,7 @@ fn isaac64(&mut self) { // abbreviations let mut a = self.a; let mut b = self.b + self.c; - const MIDPOINT: usize = RAND_SIZE_64 / 2; + const MIDPOINT: usize = RAND_SIZE_64 / 2; const MP_VEC: [(usize, usize); 2] = [(0, MIDPOINT), (MIDPOINT, 0)]; macro_rules! ind { ($x:expr) => { diff --git a/src/librand/lib.rs b/src/librand/lib.rs index cf2118b45a4..247dcd03b5d 100644 --- a/src/librand/lib.rs +++ b/src/librand/lib.rs @@ -306,10 +306,9 @@ impl<'a, R: Rng> Iterator for AsciiGenerator<'a, R> { type Item = char; fn next(&mut self) -> Option { - const GEN_ASCII_STR_CHARSET: &'static [u8] = - b"ABCDEFGHIJKLMNOPQRSTUVWXYZ\ - abcdefghijklmnopqrstuvwxyz\ - 0123456789"; + const GEN_ASCII_STR_CHARSET: &'static [u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ\ + abcdefghijklmnopqrstuvwxyz\ + 0123456789"; Some(*self.rng.choose(GEN_ASCII_STR_CHARSET).unwrap() as char) } } diff --git a/src/librand/rand_impls.rs b/src/librand/rand_impls.rs index 726a4554626..34b7f37a678 100644 --- a/src/librand/rand_impls.rs +++ b/src/librand/rand_impls.rs @@ -202,7 +202,7 @@ fn rand(_: &mut R) -> () { tuple_impl!{A, B, C, D, E, F, G, H, I, J, K} tuple_impl!{A, B, C, D, E, F, G, H, I, J, K, L} -impl Rand for Option { +impl Rand for Option { #[inline] fn rand(rng: &mut R) -> Option { if rng.gen() {