зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1483288 - Provide more number conversions for mozprofile::preferences::PrefValue. r=whimboo
Allow i8, u8, i16, u64, i32, and u32 to be implicitly converted into PrefValue::Int. u64 is not supported because it would overflow, so this still needs to be handled manually. geckodriver stores the port number as u8 and this will allow it to implicitly convert it to PrefValue::Int without using the unsafe "as i64" coercion.
This commit is contained in:
Родитель
30c2ae52a8
Коммит
8f5737840d
|
@ -1,13 +1,11 @@
|
|||
use std::collections::BTreeMap;
|
||||
use std::string;
|
||||
use std::convert::From;
|
||||
|
||||
pub type Preferences = BTreeMap<String, Pref>;
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub enum PrefValue {
|
||||
Bool(bool),
|
||||
String(string::String),
|
||||
String(String),
|
||||
Int(i64),
|
||||
}
|
||||
|
||||
|
@ -29,32 +27,102 @@ impl From<&'static str> for PrefValue {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<i8> for PrefValue {
|
||||
fn from(value: i8) -> Self {
|
||||
PrefValue::Int(value.into())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<u8> for PrefValue {
|
||||
fn from(value: u8) -> Self {
|
||||
PrefValue::Int(value.into())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<i16> for PrefValue {
|
||||
fn from(value: i16) -> Self {
|
||||
PrefValue::Int(value.into())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<u16> for PrefValue {
|
||||
fn from(value: u16) -> Self {
|
||||
PrefValue::Int(value.into())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<i32> for PrefValue {
|
||||
fn from(value: i32) -> Self {
|
||||
PrefValue::Int(value.into())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<u32> for PrefValue {
|
||||
fn from(value: u32) -> Self {
|
||||
PrefValue::Int(value.into())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<i64> for PrefValue {
|
||||
fn from(value: i64) -> Self {
|
||||
PrefValue::Int(value)
|
||||
}
|
||||
}
|
||||
|
||||
// Implementing From<u64> for PrefValue wouldn't be safe
|
||||
// because it might overflow.
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub struct Pref {
|
||||
pub value: PrefValue,
|
||||
pub sticky: bool
|
||||
pub sticky: bool,
|
||||
}
|
||||
|
||||
impl Pref {
|
||||
pub fn new<T>(value: T) -> Pref
|
||||
where T: Into<PrefValue> {
|
||||
where
|
||||
T: Into<PrefValue>,
|
||||
{
|
||||
Pref {
|
||||
value: value.into(),
|
||||
sticky: false
|
||||
sticky: false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_sticky<T>(value: T) -> Pref
|
||||
where T: Into<PrefValue> {
|
||||
where
|
||||
T: Into<PrefValue>,
|
||||
{
|
||||
Pref {
|
||||
value: value.into(),
|
||||
sticky: true
|
||||
sticky: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::PrefValue;
|
||||
|
||||
#[test]
|
||||
fn test_bool() {
|
||||
assert_eq!(PrefValue::from(true), PrefValue::Bool(true));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_string() {
|
||||
assert_eq!(PrefValue::from("foo"), PrefValue::String("foo".to_string()));
|
||||
assert_eq!(PrefValue::from("foo".to_string()), PrefValue::String("foo".to_string()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_int() {
|
||||
assert_eq!(PrefValue::from(42i8), PrefValue::Int(42i64));
|
||||
assert_eq!(PrefValue::from(42u8), PrefValue::Int(42i64));
|
||||
assert_eq!(PrefValue::from(42i16), PrefValue::Int(42i64));
|
||||
assert_eq!(PrefValue::from(42u16), PrefValue::Int(42i64));
|
||||
assert_eq!(PrefValue::from(42i32), PrefValue::Int(42i64));
|
||||
assert_eq!(PrefValue::from(42u32), PrefValue::Int(42i64));
|
||||
assert_eq!(PrefValue::from(42i64), PrefValue::Int(42i64));
|
||||
}
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче