Made `Parsed` not fully destructible.

So that we can add more fields without breaking a major compatibility
(as per RFC 1105).
This commit is contained in:
Kang Seonghoon 2017-02-06 08:24:16 +09:00
parent 36c45f2e02
commit 2b5553ee76
2 changed files with 22 additions and 8 deletions

View File

@ -358,9 +358,11 @@ fn test_parse() {
($fmt:expr, $items:expr; $err:tt) => (
assert_eq!(parse_all($fmt, &$items), Err($err))
);
($fmt:expr, $items:expr; $($k:ident: $v:expr),*) => (
assert_eq!(parse_all($fmt, &$items), Ok(Parsed { $($k: Some($v),)* ..Parsed::new() }))
);
($fmt:expr, $items:expr; $($k:ident: $v:expr),*) => (#[allow(unused_mut)] {
let mut expected = Parsed::new();
$(expected.$k = Some($v);)*
assert_eq!(parse_all($fmt, &$items), Ok(expected))
});
}
// empty string

View File

@ -108,6 +108,9 @@ pub struct Parsed {
/// Offset from the local time to UTC, in seconds.
pub offset: Option<i32>,
/// A dummy field to make this type not fully destructible (required for API stability).
_dummy: (),
}
/// Checks if `old` is either empty or has the same value to `new` (i.e. "consistent"),
@ -121,14 +124,23 @@ fn set_if_consistent<T: PartialEq>(old: &mut Option<T>, new: T) -> ParseResult<(
}
}
impl Parsed {
/// Returns the initial value of parsed parts.
pub fn new() -> Parsed {
Parsed { year: None, year_div_100: None, year_mod_100: None, isoyear: None,
impl Default for Parsed {
fn default() -> Parsed {
Parsed {
year: None, year_div_100: None, year_mod_100: None, isoyear: None,
isoyear_div_100: None, isoyear_mod_100: None, month: None,
week_from_sun: None, week_from_mon: None, isoweek: None, weekday: None,
ordinal: None, day: None, hour_div_12: None, hour_mod_12: None, minute: None,
second: None, nanosecond: None, timestamp: None, offset: None }
second: None, nanosecond: None, timestamp: None, offset: None,
_dummy: (),
}
}
}
impl Parsed {
/// Returns the initial value of parsed parts.
pub fn new() -> Parsed {
Parsed::default()
}
/// Tries to set the [`year`](#structfield.year) field from given value.