Skip to content

Commit d9438c3

Browse files
committed
Add ToString and FromStr impls for Epoch
1 parent b1f8e6f commit d9438c3

File tree

1 file changed

+33
-5
lines changed

1 file changed

+33
-5
lines changed

src/librustc/session/config.rs

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use std::collections::btree_map::Iter as BTreeMapIter;
4141
use std::collections::btree_map::Keys as BTreeMapKeysIter;
4242
use std::collections::btree_map::Values as BTreeMapValuesIter;
4343

44-
use std::fmt;
44+
use std::{fmt, str};
4545
use std::hash::Hasher;
4646
use std::collections::hash_map::DefaultHasher;
4747
use std::collections::HashSet;
@@ -137,6 +137,28 @@ pub enum Epoch {
137137
// as well as changing the default Cargo template.
138138
}
139139

140+
pub const ALL_EPOCHS: &[Epoch] = &[Epoch::Epoch2015, Epoch::Epoch2018];
141+
142+
impl ToString for Epoch {
143+
fn to_string(&self) -> String {
144+
match *self {
145+
Epoch::Epoch2015 => "2015".into(),
146+
Epoch::Epoch2018 => "2018".into(),
147+
}
148+
}
149+
}
150+
151+
impl str::FromStr for Epoch {
152+
type Err = ();
153+
fn from_str(s: &str) -> Result<Self, ()> {
154+
match s {
155+
"2015" => Ok(Epoch::Epoch2015),
156+
"2018" => Ok(Epoch::Epoch2018),
157+
_ => Err(())
158+
}
159+
}
160+
}
161+
140162
impl_stable_hash_for!(enum self::OutputType {
141163
Bitcode,
142164
Assembly,
@@ -1021,11 +1043,17 @@ macro_rules! options {
10211043

10221044
fn parse_epoch(slot: &mut Epoch, v: Option<&str>) -> bool {
10231045
match v {
1024-
Some("2015") => *slot = Epoch::Epoch2015,
1025-
Some("2018") => *slot = Epoch::Epoch2018,
1026-
_ => return false,
1046+
Some(s) => {
1047+
let epoch = s.parse();
1048+
if let Ok(parsed) = epoch {
1049+
*slot = parsed;
1050+
true
1051+
} else {
1052+
false
1053+
}
1054+
}
1055+
_ => false,
10271056
}
1028-
true
10291057
}
10301058
}
10311059
) }

0 commit comments

Comments
 (0)