diff options
| author | bors <bors@rust-lang.org> | 2013-09-14 00:01:04 -0700 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2013-09-14 00:01:04 -0700 |
| commit | 2aa578efd9834e37ad52879ff10ee2c2aa938389 (patch) | |
| tree | aa459cb42c65433dfbddca28040db544aea89f59 /src/libextra | |
| parent | 4ac10f8f6e8e07c70fadb676170c5402442e2243 (diff) | |
| parent | 93683ae6da3a47f1cd0644a093cb4b1b0bee7faa (diff) | |
| download | rust-2aa578efd9834e37ad52879ff10ee2c2aa938389.tar.gz rust-2aa578efd9834e37ad52879ff10ee2c2aa938389.zip | |
auto merge of #9115 : erickt/rust/master, r=erickt
This is a series of patches to modernize option and result. The highlights are: * rename `.unwrap_or_default(value)` and etc to `.unwrap_or(value)` * add `.unwrap_or_default()` that uses the `Default` trait * add `Default` implementations for vecs, HashMap, Option * add `Option.and(T) -> Option<T>`, `Option.and_then(&fn() -> Option<T>) -> Option<T>`, `Option.or(T) -> Option<T>`, and `Option.or_else(&fn() -> Option<T>) -> Option<T>` * add `option::ToOption`, `option::IntoOption`, `option::AsOption`, `result::ToResult`, `result::IntoResult`, `result::AsResult`, `either::ToEither`, and `either::IntoEither`, `either::AsEither` * renamed `Option::chain*` and `Result::chain*` to `and_then` and `or_else` to avoid the eventual collision with `Iterator.chain`. * Added a bunch of impls of `Default` * Added a `#[deriving(Default)]` syntax extension * Removed impls of `Zero` for `Option<T>` and vecs.
Diffstat (limited to 'src/libextra')
| -rw-r--r-- | src/libextra/glob.rs | 46 | ||||
| -rw-r--r-- | src/libextra/num/bigint.rs | 4 | ||||
| -rw-r--r-- | src/libextra/num/rational.rs | 8 | ||||
| -rw-r--r-- | src/libextra/time.rs | 64 | ||||
| -rw-r--r-- | src/libextra/uuid.rs | 7 |
5 files changed, 88 insertions, 41 deletions
diff --git a/src/libextra/glob.rs b/src/libextra/glob.rs index 2cd0fdd4ede..39a4ac61846 100644 --- a/src/libextra/glob.rs +++ b/src/libextra/glob.rs @@ -137,7 +137,17 @@ fn list_dir_sorted(path: &Path) -> ~[Path] { /** * A compiled Unix shell style pattern. */ -#[deriving(Clone, Eq, TotalEq, Ord, TotalOrd, IterBytes, Zero)] +#[cfg(stage0)] +#[deriving(Clone, Eq, TotalEq, Ord, TotalOrd, IterBytes)] +pub struct Pattern { + priv tokens: ~[PatternToken] +} + +/** + * A compiled Unix shell style pattern. + */ +#[cfg(not(stage0))] +#[deriving(Clone, Eq, TotalEq, Ord, TotalOrd, IterBytes, Default)] pub struct Pattern { priv tokens: ~[PatternToken] } @@ -312,7 +322,7 @@ impl Pattern { let require_literal = |c| { (options.require_literal_separator && is_sep(c)) || (options.require_literal_leading_dot && c == '.' - && is_sep(prev_char.unwrap_or_default('/'))) + && is_sep(prev_char.unwrap_or('/'))) }; for (ti, token) in self.tokens.slice_from(i).iter().enumerate() { @@ -458,7 +468,37 @@ fn is_sep(c: char) -> bool { /** * Configuration options to modify the behaviour of `Pattern::matches_with(..)` */ -#[deriving(Clone, Eq, TotalEq, Ord, TotalOrd, IterBytes, Zero)] +#[cfg(stage0)] +#[deriving(Clone, Eq, TotalEq, Ord, TotalOrd, IterBytes)] +pub struct MatchOptions { + + /** + * Whether or not patterns should be matched in a case-sensitive manner. This + * currently only considers upper/lower case relationships between ASCII characters, + * but in future this might be extended to work with Unicode. + */ + case_sensitive: bool, + + /** + * If this is true then path-component separator characters (e.g. `/` on Posix) + * must be matched by a literal `/`, rather than by `*` or `?` or `[...]` + */ + require_literal_separator: bool, + + /** + * If this is true then paths that contain components that start with a `.` will + * not match unless the `.` appears literally in the pattern: `*`, `?` or `[...]` + * will not match. This is useful because such files are conventionally considered + * hidden on Unix systems and it might be desirable to skip them when listing files. + */ + require_literal_leading_dot: bool +} + +/** + * Configuration options to modify the behaviour of `Pattern::matches_with(..)` + */ +#[cfg(not(stage0))] +#[deriving(Clone, Eq, TotalEq, Ord, TotalOrd, IterBytes, Default)] pub struct MatchOptions { /** diff --git a/src/libextra/num/bigint.rs b/src/libextra/num/bigint.rs index 8975ed7fd96..24f44c8a2a8 100644 --- a/src/libextra/num/bigint.rs +++ b/src/libextra/num/bigint.rs @@ -635,7 +635,7 @@ impl BigUint { // Converts this BigUint into an int, unless it would overflow. pub fn to_int_opt(&self) -> Option<int> { - self.to_uint_opt().chain(|n| { + self.to_uint_opt().and_then(|n| { // If top bit of uint is set, it's too large to convert to // int. if (n >> (2*BigDigit::bits - 1) != 0) { @@ -1221,7 +1221,7 @@ impl BigInt { match self.sign { Plus => self.data.to_int_opt(), Zero => Some(0), - Minus => self.data.to_uint_opt().chain(|n| { + Minus => self.data.to_uint_opt().and_then(|n| { let m: uint = 1 << (2*BigDigit::bits-1); if (n > m) { None diff --git a/src/libextra/num/rational.rs b/src/libextra/num/rational.rs index 41e9a488bf8..1991d9f1b5b 100644 --- a/src/libextra/num/rational.rs +++ b/src/libextra/num/rational.rs @@ -273,9 +273,9 @@ impl<T: FromStr + Clone + Integer + Ord> return None } let a_option: Option<T> = FromStr::from_str(split[0]); - do a_option.chain |a| { + do a_option.and_then |a| { let b_option: Option<T> = FromStr::from_str(split[1]); - do b_option.chain |b| { + do b_option.and_then |b| { Some(Ratio::new(a.clone(), b.clone())) } } @@ -291,10 +291,10 @@ impl<T: FromStrRadix + Clone + Integer + Ord> } else { let a_option: Option<T> = FromStrRadix::from_str_radix(split[0], radix); - do a_option.chain |a| { + do a_option.and_then |a| { let b_option: Option<T> = FromStrRadix::from_str_radix(split[1], radix); - do b_option.chain |b| { + do b_option.and_then |b| { Some(Ratio::new(a.clone(), b.clone())) } } diff --git a/src/libextra/time.rs b/src/libextra/time.rs index 7515326a0db..d51d1c21785 100644 --- a/src/libextra/time.rs +++ b/src/libextra/time.rs @@ -442,21 +442,21 @@ fn do_strptime(s: &str, format: &str) -> Result<Tm, ~str> { }, 'c' => { parse_type(s, pos, 'a', &mut *tm) - .chain(|pos| parse_char(s, pos, ' ')) - .chain(|pos| parse_type(s, pos, 'b', &mut *tm)) - .chain(|pos| parse_char(s, pos, ' ')) - .chain(|pos| parse_type(s, pos, 'e', &mut *tm)) - .chain(|pos| parse_char(s, pos, ' ')) - .chain(|pos| parse_type(s, pos, 'T', &mut *tm)) - .chain(|pos| parse_char(s, pos, ' ')) - .chain(|pos| parse_type(s, pos, 'Y', &mut *tm)) + .and_then(|pos| parse_char(s, pos, ' ')) + .and_then(|pos| parse_type(s, pos, 'b', &mut *tm)) + .and_then(|pos| parse_char(s, pos, ' ')) + .and_then(|pos| parse_type(s, pos, 'e', &mut *tm)) + .and_then(|pos| parse_char(s, pos, ' ')) + .and_then(|pos| parse_type(s, pos, 'T', &mut *tm)) + .and_then(|pos| parse_char(s, pos, ' ')) + .and_then(|pos| parse_type(s, pos, 'Y', &mut *tm)) } 'D' | 'x' => { parse_type(s, pos, 'm', &mut *tm) - .chain(|pos| parse_char(s, pos, '/')) - .chain(|pos| parse_type(s, pos, 'd', &mut *tm)) - .chain(|pos| parse_char(s, pos, '/')) - .chain(|pos| parse_type(s, pos, 'y', &mut *tm)) + .and_then(|pos| parse_char(s, pos, '/')) + .and_then(|pos| parse_type(s, pos, 'd', &mut *tm)) + .and_then(|pos| parse_char(s, pos, '/')) + .and_then(|pos| parse_type(s, pos, 'y', &mut *tm)) } 'd' => match match_digits_in_range(s, pos, 2u, false, 1_i32, 31_i32) { @@ -475,10 +475,10 @@ fn do_strptime(s: &str, format: &str) -> Result<Tm, ~str> { } 'F' => { parse_type(s, pos, 'Y', &mut *tm) - .chain(|pos| parse_char(s, pos, '-')) - .chain(|pos| parse_type(s, pos, 'm', &mut *tm)) - .chain(|pos| parse_char(s, pos, '-')) - .chain(|pos| parse_type(s, pos, 'd', &mut *tm)) + .and_then(|pos| parse_char(s, pos, '-')) + .and_then(|pos| parse_type(s, pos, 'm', &mut *tm)) + .and_then(|pos| parse_char(s, pos, '-')) + .and_then(|pos| parse_type(s, pos, 'd', &mut *tm)) } 'H' => { match match_digits_in_range(s, pos, 2u, false, 0_i32, 23_i32) { @@ -553,17 +553,17 @@ fn do_strptime(s: &str, format: &str) -> Result<Tm, ~str> { }, 'R' => { parse_type(s, pos, 'H', &mut *tm) - .chain(|pos| parse_char(s, pos, ':')) - .chain(|pos| parse_type(s, pos, 'M', &mut *tm)) + .and_then(|pos| parse_char(s, pos, ':')) + .and_then(|pos| parse_type(s, pos, 'M', &mut *tm)) } 'r' => { parse_type(s, pos, 'I', &mut *tm) - .chain(|pos| parse_char(s, pos, ':')) - .chain(|pos| parse_type(s, pos, 'M', &mut *tm)) - .chain(|pos| parse_char(s, pos, ':')) - .chain(|pos| parse_type(s, pos, 'S', &mut *tm)) - .chain(|pos| parse_char(s, pos, ' ')) - .chain(|pos| parse_type(s, pos, 'p', &mut *tm)) + .and_then(|pos| parse_char(s, pos, ':')) + .and_then(|pos| parse_type(s, pos, 'M', &mut *tm)) + .and_then(|pos| parse_char(s, pos, ':')) + .and_then(|pos| parse_type(s, pos, 'S', &mut *tm)) + .and_then(|pos| parse_char(s, pos, ' ')) + .and_then(|pos| parse_type(s, pos, 'p', &mut *tm)) } 'S' => { match match_digits_in_range(s, pos, 2u, false, 0_i32, 60_i32) { @@ -578,10 +578,10 @@ fn do_strptime(s: &str, format: &str) -> Result<Tm, ~str> { //'s' {} 'T' | 'X' => { parse_type(s, pos, 'H', &mut *tm) - .chain(|pos| parse_char(s, pos, ':')) - .chain(|pos| parse_type(s, pos, 'M', &mut *tm)) - .chain(|pos| parse_char(s, pos, ':')) - .chain(|pos| parse_type(s, pos, 'S', &mut *tm)) + .and_then(|pos| parse_char(s, pos, ':')) + .and_then(|pos| parse_type(s, pos, 'M', &mut *tm)) + .and_then(|pos| parse_char(s, pos, ':')) + .and_then(|pos| parse_type(s, pos, 'S', &mut *tm)) } 't' => parse_char(s, pos, '\t'), 'u' => { @@ -596,10 +596,10 @@ fn do_strptime(s: &str, format: &str) -> Result<Tm, ~str> { } 'v' => { parse_type(s, pos, 'e', &mut *tm) - .chain(|pos| parse_char(s, pos, '-')) - .chain(|pos| parse_type(s, pos, 'b', &mut *tm)) - .chain(|pos| parse_char(s, pos, '-')) - .chain(|pos| parse_type(s, pos, 'Y', &mut *tm)) + .and_then(|pos| parse_char(s, pos, '-')) + .and_then(|pos| parse_type(s, pos, 'b', &mut *tm)) + .and_then(|pos| parse_char(s, pos, '-')) + .and_then(|pos| parse_type(s, pos, 'Y', &mut *tm)) } //'W' {} 'w' => { diff --git a/src/libextra/uuid.rs b/src/libextra/uuid.rs index b2c0f24824f..2641c1379e4 100644 --- a/src/libextra/uuid.rs +++ b/src/libextra/uuid.rs @@ -417,6 +417,13 @@ impl Uuid { } } +impl Default for Uuid { + /// Returns the nil UUID, which is all zeroes + fn default() -> Uuid { + Uuid::new_nil() + } +} + impl Zero for Uuid { /// Returns the nil UUID, which is all zeroes fn zero() -> Uuid { |
