diff --git a/Cargo.toml b/Cargo.toml index a1f38af7..351517d8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,9 @@ license = "MIT/Apache-2.0" description = "A serialization/deserialization framework" repository = "https://github.com/erickt/rust-serde" +[dependencies] +num = "*" + [dev-dependencies] rustc-serialize = "*" diff --git a/benches/bench_log.rs b/benches/bench_log.rs index 536ce406..e29a3168 100644 --- a/benches/bench_log.rs +++ b/benches/bench_log.rs @@ -1,13 +1,14 @@ -#![feature(custom_derive, collections, core, plugin, test)] +#![feature(custom_derive, collections, plugin, test)] #![allow(non_camel_case_types)] #![plugin(serde_macros)] -extern crate serde; +extern crate num; extern crate rustc_serialize; +extern crate serde; extern crate test; use std::io::{self, Read, Write}; -use std::num::FromPrimitive; +use num::FromPrimitive; use test::Bencher; use serde::de::{self, Deserialize, Deserializer}; @@ -30,7 +31,7 @@ struct Http { request_uri: String, } -#[derive(Copy, Clone, Debug, PartialEq, FromPrimitive)] +#[derive(Copy, Clone, Debug, PartialEq)] enum HttpProtocol { HTTP_PROTOCOL_UNKNOWN, HTTP10, @@ -52,6 +53,21 @@ impl rustc_serialize::Decodable for HttpProtocol { } } +impl FromPrimitive for HttpProtocol { + fn from_i64(i: i64) -> Option { + FromPrimitive::from_u64(i as u64) + } + + fn from_u64(n: u64) -> Option { + match n { + 0 => Some(HttpProtocol::HTTP_PROTOCOL_UNKNOWN), + 1 => Some(HttpProtocol::HTTP10), + 2 => Some(HttpProtocol::HTTP11), + _ => None, + } + } +} + impl ser::Serialize for HttpProtocol { #[inline] fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> @@ -70,7 +86,7 @@ impl de::Deserialize for HttpProtocol { } } -#[derive(Copy, Clone, Debug, PartialEq, FromPrimitive)] +#[derive(Copy, Clone, Debug, PartialEq)] enum HttpMethod { METHOD_UNKNOWN, GET, @@ -85,6 +101,29 @@ enum HttpMethod { PATCH, } +impl FromPrimitive for HttpMethod { + fn from_i64(i: i64) -> Option { + FromPrimitive::from_u64(i as u64) + } + + fn from_u64(n: u64) -> Option { + match n { + 0 => Some(HttpMethod::METHOD_UNKNOWN), + 1 => Some(HttpMethod::GET), + 2 => Some(HttpMethod::POST), + 3 => Some(HttpMethod::DELETE), + 4 => Some(HttpMethod::PUT), + 5 => Some(HttpMethod::HEAD), + 6 => Some(HttpMethod::PURGE), + 7 => Some(HttpMethod::OPTIONS), + 8 => Some(HttpMethod::PROPFIND), + 9 => Some(HttpMethod::MKCOL), + 10 => Some(HttpMethod::PATCH), + _ => None, + } + } +} + impl rustc_serialize::Encodable for HttpMethod { fn encode(&self, s: &mut S) -> Result<(), S::Error> { (*self as usize).encode(s) @@ -118,7 +157,7 @@ impl de::Deserialize for HttpMethod { } } -#[derive(Copy, Clone, Debug, PartialEq, FromPrimitive)] +#[derive(Copy, Clone, Debug, PartialEq)] enum CacheStatus { CACHESTATUS_UNKNOWN, Miss, @@ -126,6 +165,22 @@ enum CacheStatus { Hit, } +impl FromPrimitive for CacheStatus { + fn from_i64(i: i64) -> Option { + FromPrimitive::from_u64(i as u64) + } + + fn from_u64(n: u64) -> Option { + match n { + 0 => Some(CacheStatus::CACHESTATUS_UNKNOWN), + 1 => Some(CacheStatus::Miss), + 2 => Some(CacheStatus::Expired), + 3 => Some(CacheStatus::Hit), + _ => None, + } + } +} + impl rustc_serialize::Encodable for CacheStatus { fn encode(&self, s: &mut S) -> Result<(), S::Error> { (*self as u8).encode(s) @@ -167,13 +222,28 @@ struct Origin { protocol: OriginProtocol, } -#[derive(Copy, Clone, Debug, PartialEq, FromPrimitive)] +#[derive(Copy, Clone, Debug, PartialEq)] enum OriginProtocol { ORIGIN_PROTOCOL_UNKNOWN, HTTP, HTTPS, } +impl FromPrimitive for OriginProtocol { + fn from_i64(i: i64) -> Option { + FromPrimitive::from_u64(i as u64) + } + + fn from_u64(n: u64) -> Option { + match n { + 0 => Some(OriginProtocol::ORIGIN_PROTOCOL_UNKNOWN), + 1 => Some(OriginProtocol::HTTP), + 2 => Some(OriginProtocol::HTTPS), + _ => None, + } + } +} + impl rustc_serialize::Encodable for OriginProtocol { fn encode(&self, s: &mut S) -> Result<(), S::Error> { (*self as u8).encode(s) @@ -207,7 +277,7 @@ impl de::Deserialize for OriginProtocol { } } -#[derive(Copy, Clone, Debug, PartialEq, FromPrimitive)] +#[derive(Copy, Clone, Debug, PartialEq)] enum ZonePlan { ZONEPLAN_UNKNOWN, FREE, @@ -216,6 +286,23 @@ enum ZonePlan { ENT, } +impl FromPrimitive for ZonePlan { + fn from_i64(i: i64) -> Option { + FromPrimitive::from_u64(i as u64) + } + + fn from_u64(n: u64) -> Option { + match n { + 0 => Some(ZonePlan::ZONEPLAN_UNKNOWN), + 1 => Some(ZonePlan::FREE), + 2 => Some(ZonePlan::PRO), + 3 => Some(ZonePlan::BIZ), + 4 => Some(ZonePlan::ENT), + _ => None, + } + } +} + impl rustc_serialize::Encodable for ZonePlan { fn encode(&self, s: &mut S) -> Result<(), S::Error> { (*self as u8).encode(s) @@ -249,7 +336,7 @@ impl de::Deserialize for ZonePlan { } } -#[derive(Copy, Clone, Debug, PartialEq, FromPrimitive)] +#[derive(Copy, Clone, Debug, PartialEq)] enum Country { UNKNOWN, A1, @@ -509,6 +596,274 @@ enum Country { ZW, } +impl FromPrimitive for Country { + fn from_i64(i: i64) -> Option { + FromPrimitive::from_u64(i as u64) + } + + fn from_u64(n: u64) -> Option { + match n { + 0 => Some(Country::UNKNOWN), + 1 => Some(Country::A1), + 2 => Some(Country::A2), + 3 => Some(Country::O1), + 4 => Some(Country::AD), + 5 => Some(Country::AE), + 6 => Some(Country::AF), + 7 => Some(Country::AG), + 8 => Some(Country::AI), + 9 => Some(Country::AL), + 10 => Some(Country::AM), + 11 => Some(Country::AO), + 12 => Some(Country::AP), + 13 => Some(Country::AQ), + 14 => Some(Country::AR), + 15 => Some(Country::AS), + 16 => Some(Country::AT), + 17 => Some(Country::AU), + 18 => Some(Country::AW), + 19 => Some(Country::AX), + 20 => Some(Country::AZ), + 21 => Some(Country::BA), + 22 => Some(Country::BB), + 23 => Some(Country::BD), + 24 => Some(Country::BE), + 25 => Some(Country::BF), + 26 => Some(Country::BG), + 27 => Some(Country::BH), + 28 => Some(Country::BI), + 29 => Some(Country::BJ), + 30 => Some(Country::BL), + 31 => Some(Country::BM), + 32 => Some(Country::BN), + 33 => Some(Country::BO), + 34 => Some(Country::BQ), + 35 => Some(Country::BR), + 36 => Some(Country::BS), + 37 => Some(Country::BT), + 38 => Some(Country::BV), + 39 => Some(Country::BW), + 40 => Some(Country::BY), + 41 => Some(Country::BZ), + 42 => Some(Country::CA), + 43 => Some(Country::CC), + 44 => Some(Country::CD), + 45 => Some(Country::CF), + 46 => Some(Country::CG), + 47 => Some(Country::CH), + 48 => Some(Country::CI), + 49 => Some(Country::CK), + 50 => Some(Country::CL), + 51 => Some(Country::CM), + 52 => Some(Country::CN), + 53 => Some(Country::CO), + 54 => Some(Country::CR), + 55 => Some(Country::CU), + 56 => Some(Country::CV), + 57 => Some(Country::CW), + 58 => Some(Country::CX), + 59 => Some(Country::CY), + 60 => Some(Country::CZ), + 61 => Some(Country::DE), + 62 => Some(Country::DJ), + 63 => Some(Country::DK), + 64 => Some(Country::DM), + 65 => Some(Country::DO), + 66 => Some(Country::DZ), + 67 => Some(Country::EC), + 68 => Some(Country::EE), + 69 => Some(Country::EG), + 70 => Some(Country::EH), + 71 => Some(Country::ER), + 72 => Some(Country::ES), + 73 => Some(Country::ET), + 74 => Some(Country::EU), + 75 => Some(Country::FI), + 76 => Some(Country::FJ), + 77 => Some(Country::FK), + 78 => Some(Country::FM), + 79 => Some(Country::FO), + 80 => Some(Country::FR), + 81 => Some(Country::GA), + 82 => Some(Country::GB), + 83 => Some(Country::GD), + 84 => Some(Country::GE), + 85 => Some(Country::GF), + 86 => Some(Country::GG), + 87 => Some(Country::GH), + 88 => Some(Country::GI), + 89 => Some(Country::GL), + 90 => Some(Country::GM), + 91 => Some(Country::GN), + 92 => Some(Country::GP), + 93 => Some(Country::GQ), + 94 => Some(Country::GR), + 95 => Some(Country::GS), + 96 => Some(Country::GT), + 97 => Some(Country::GU), + 98 => Some(Country::GW), + 99 => Some(Country::GY), + 100 => Some(Country::HK), + 101 => Some(Country::HM), + 102 => Some(Country::HN), + 103 => Some(Country::HR), + 104 => Some(Country::HT), + 105 => Some(Country::HU), + 106 => Some(Country::ID), + 107 => Some(Country::IE), + 108 => Some(Country::IL), + 109 => Some(Country::IM), + 110 => Some(Country::IN), + 111 => Some(Country::IO), + 112 => Some(Country::IQ), + 113 => Some(Country::IR), + 114 => Some(Country::IS), + 115 => Some(Country::IT), + 116 => Some(Country::JE), + 117 => Some(Country::JM), + 118 => Some(Country::JO), + 119 => Some(Country::JP), + 120 => Some(Country::KE), + 121 => Some(Country::KG), + 122 => Some(Country::KH), + 123 => Some(Country::KI), + 124 => Some(Country::KM), + 125 => Some(Country::KN), + 126 => Some(Country::KP), + 127 => Some(Country::KR), + 128 => Some(Country::KW), + 129 => Some(Country::KY), + 130 => Some(Country::KZ), + 131 => Some(Country::LA), + 132 => Some(Country::LB), + 133 => Some(Country::LC), + 134 => Some(Country::LI), + 135 => Some(Country::LK), + 136 => Some(Country::LR), + 137 => Some(Country::LS), + 138 => Some(Country::LT), + 139 => Some(Country::LU), + 140 => Some(Country::LV), + 141 => Some(Country::LY), + 142 => Some(Country::MA), + 143 => Some(Country::MC), + 144 => Some(Country::MD), + 145 => Some(Country::ME), + 146 => Some(Country::MF), + 147 => Some(Country::MG), + 148 => Some(Country::MH), + 149 => Some(Country::MK), + 150 => Some(Country::ML), + 151 => Some(Country::MM), + 152 => Some(Country::MN), + 153 => Some(Country::MO), + 154 => Some(Country::MP), + 155 => Some(Country::MQ), + 156 => Some(Country::MR), + 157 => Some(Country::MS), + 158 => Some(Country::MT), + 159 => Some(Country::MU), + 160 => Some(Country::MV), + 161 => Some(Country::MW), + 162 => Some(Country::MX), + 163 => Some(Country::MY), + 164 => Some(Country::MZ), + 165 => Some(Country::NA), + 166 => Some(Country::NC), + 167 => Some(Country::NE), + 168 => Some(Country::NF), + 169 => Some(Country::NG), + 170 => Some(Country::NI), + 171 => Some(Country::NL), + 172 => Some(Country::NO), + 173 => Some(Country::NP), + 174 => Some(Country::NR), + 175 => Some(Country::NU), + 176 => Some(Country::NZ), + 177 => Some(Country::OM), + 178 => Some(Country::PA), + 179 => Some(Country::PE), + 180 => Some(Country::PF), + 181 => Some(Country::PG), + 182 => Some(Country::PH), + 183 => Some(Country::PK), + 184 => Some(Country::PL), + 185 => Some(Country::PM), + 186 => Some(Country::PN), + 187 => Some(Country::PR), + 188 => Some(Country::PS), + 189 => Some(Country::PT), + 190 => Some(Country::PW), + 191 => Some(Country::PY), + 192 => Some(Country::QA), + 193 => Some(Country::RE), + 194 => Some(Country::RO), + 195 => Some(Country::RS), + 196 => Some(Country::RU), + 197 => Some(Country::RW), + 198 => Some(Country::SA), + 199 => Some(Country::SB), + 200 => Some(Country::SC), + 201 => Some(Country::SD), + 202 => Some(Country::SE), + 203 => Some(Country::SG), + 204 => Some(Country::SH), + 205 => Some(Country::SI), + 206 => Some(Country::SJ), + 207 => Some(Country::SK), + 208 => Some(Country::SL), + 209 => Some(Country::SM), + 210 => Some(Country::SN), + 211 => Some(Country::SO), + 212 => Some(Country::SR), + 213 => Some(Country::SS), + 214 => Some(Country::ST), + 215 => Some(Country::SV), + 216 => Some(Country::SX), + 217 => Some(Country::SY), + 218 => Some(Country::SZ), + 219 => Some(Country::TC), + 220 => Some(Country::TD), + 221 => Some(Country::TF), + 222 => Some(Country::TG), + 223 => Some(Country::TH), + 224 => Some(Country::TJ), + 225 => Some(Country::TK), + 226 => Some(Country::TL), + 227 => Some(Country::TM), + 228 => Some(Country::TN), + 229 => Some(Country::TO), + 230 => Some(Country::TR), + 231 => Some(Country::TT), + 232 => Some(Country::TV), + 233 => Some(Country::TW), + 234 => Some(Country::TZ), + 235 => Some(Country::UA), + 236 => Some(Country::UG), + 237 => Some(Country::UM), + 238 => Some(Country::US), + 239 => Some(Country::UY), + 240 => Some(Country::UZ), + 241 => Some(Country::VA), + 242 => Some(Country::VC), + 243 => Some(Country::VE), + 244 => Some(Country::VG), + 245 => Some(Country::VI), + 246 => Some(Country::VN), + 247 => Some(Country::VU), + 248 => Some(Country::WF), + 249 => Some(Country::WS), + 250 => Some(Country::XX), + 251 => Some(Country::YE), + 252 => Some(Country::YT), + 253 => Some(Country::ZA), + 254 => Some(Country::ZM), + 255 => Some(Country::ZW), + _ => None, + } + } +} + impl rustc_serialize::Encodable for Country { fn encode(&self, s: &mut S) -> Result<(), S::Error> { (*self as u8).encode(s) diff --git a/src/de/impls.rs b/src/de/impls.rs index 53b3c16b..f73b3e52 100644 --- a/src/de/impls.rs +++ b/src/de/impls.rs @@ -1,11 +1,12 @@ use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet}; use std::hash::Hash; use std::marker::PhantomData; -use std::num::FromPrimitive; use std::path; use std::rc::Rc; use std::sync::Arc; +use num::FromPrimitive; + use de::{ Deserialize, Deserializer, diff --git a/src/json/value.rs b/src/json/value.rs index 5857f9c4..6d041343 100644 --- a/src/json/value.rs +++ b/src/json/value.rs @@ -1,10 +1,11 @@ use std::collections::{BTreeMap, btree_map}; use std::fmt; use std::io; -use std::num; use std::str; use std::vec; +use num::NumCast; + use de; use ser; use super::error::Error; @@ -165,7 +166,7 @@ impl Value { pub fn as_i64(&self) -> Option { match *self { Value::I64(n) => Some(n), - Value::U64(n) => num::cast(n), + Value::U64(n) => NumCast::from(n), _ => None } } @@ -174,7 +175,7 @@ impl Value { /// Returns None otherwise. pub fn as_u64(&self) -> Option { match *self { - Value::I64(n) => num::cast(n), + Value::I64(n) => NumCast::from(n), Value::U64(n) => Some(n), _ => None } @@ -184,8 +185,8 @@ impl Value { /// Returns None otherwise. pub fn as_f64(&self) -> Option { match *self { - Value::I64(n) => num::cast(n), - Value::U64(n) => num::cast(n), + Value::I64(n) => NumCast::from(n), + Value::U64(n) => NumCast::from(n), Value::F64(n) => Some(n), _ => None } diff --git a/src/lib.rs b/src/lib.rs index 5176b556..4d916298 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,8 +6,7 @@ //! leaving serde to perform roughly the same speed as a hand written serializer for a specific //! type. -#![feature(core)] - +extern crate num; pub use ser::{Serialize, Serializer}; pub use de::{Deserialize, Deserializer, Error};