1 //! Implementations of `RuntimeType` for all types. 2 3 use std::collections::HashMap; 4 use std::fmt; 5 use std::marker; 6 7 #[cfg(feature = "bytes")] 8 use bytes::Bytes; 9 10 #[cfg(feature = "bytes")] 11 use crate::chars::Chars; 12 use crate::descriptor::field_descriptor_proto::Type; 13 use crate::enum_or_unknown::EnumOrUnknown; 14 use crate::message_full::MessageFull; 15 use crate::reflect::runtime_type_box::RuntimeType; 16 use crate::reflect::types::ProtobufTypeBool; 17 use crate::reflect::types::ProtobufTypeBytes; 18 use crate::reflect::types::ProtobufTypeDouble; 19 use crate::reflect::types::ProtobufTypeEnumOrUnknown; 20 use crate::reflect::types::ProtobufTypeFixed32; 21 use crate::reflect::types::ProtobufTypeFixed64; 22 use crate::reflect::types::ProtobufTypeFloat; 23 use crate::reflect::types::ProtobufTypeInt32; 24 use crate::reflect::types::ProtobufTypeInt64; 25 use crate::reflect::types::ProtobufTypeMessage; 26 use crate::reflect::types::ProtobufTypeSfixed32; 27 use crate::reflect::types::ProtobufTypeSfixed64; 28 use crate::reflect::types::ProtobufTypeSint32; 29 use crate::reflect::types::ProtobufTypeSint64; 30 use crate::reflect::types::ProtobufTypeString; 31 #[cfg(feature = "bytes")] 32 use crate::reflect::types::ProtobufTypeTokioBytes; 33 #[cfg(feature = "bytes")] 34 use crate::reflect::types::ProtobufTypeTokioChars; 35 use crate::reflect::types::ProtobufTypeTrait; 36 use crate::reflect::types::ProtobufTypeUint32; 37 use crate::reflect::types::ProtobufTypeUint64; 38 use crate::reflect::value::value_ref::ReflectValueMut; 39 use crate::reflect::MessageRef; 40 use crate::reflect::ProtobufValue; 41 use crate::reflect::ReflectValueBox; 42 use crate::reflect::ReflectValueRef; 43 use crate::EnumFull; 44 use crate::UnknownValueRef; 45 46 /// `RuntimeType` is not implemented by all protobuf types directly 47 /// because it's not possible to implement `RuntimeType` for all `Message` 48 /// implementations at once: each `Message` implementation has to reimplement 49 /// all the methods again. With current strategy there's only implementation 50 /// for all messages, which is `RuntimeTypeMessage`. 51 /// 52 /// The downside is that we have to explicitly specify type parameters 53 /// in a lot of places. 54 pub trait RuntimeTypeTrait: fmt::Debug + Send + Sync + Sized + 'static { 55 /// Actual value for this type. 56 type Value: ProtobufValue + Clone + Sized + fmt::Debug + Default; 57 58 /// "Box" version of type type. runtime_type_box() -> RuntimeType59 fn runtime_type_box() -> RuntimeType; 60 61 /// Default value for this type. default_value_ref() -> ReflectValueRef<'static>62 fn default_value_ref() -> ReflectValueRef<'static>; 63 64 /// Construct a value from given reflective value. 65 /// 66 /// # Panics 67 /// 68 /// If reflective value is of incompatible type. from_value_box(value_box: ReflectValueBox) -> Result<Self::Value, ReflectValueBox>69 fn from_value_box(value_box: ReflectValueBox) -> Result<Self::Value, ReflectValueBox>; 70 71 /// Convert a value into a refletive box value. into_value_box(value: Self::Value) -> ReflectValueBox72 fn into_value_box(value: Self::Value) -> ReflectValueBox; 73 74 /// Convert a value into a ref value if possible. 75 /// 76 /// # Panics 77 /// 78 /// For message and enum. 79 // TODO: move the operation into a separate trait into_static_value_ref(value: Self::Value) -> ReflectValueRef<'static>80 fn into_static_value_ref(value: Self::Value) -> ReflectValueRef<'static> { 81 panic!("value {:?} cannot be converted to static ref", value) 82 } 83 84 /// Pointer to a dynamic reference. as_ref(value: &Self::Value) -> ReflectValueRef85 fn as_ref(value: &Self::Value) -> ReflectValueRef; 86 /// Mutable pointer to a dynamic mutable reference. as_mut(value: &mut Self::Value) -> ReflectValueMut87 fn as_mut(value: &mut Self::Value) -> ReflectValueMut; 88 89 /// Value is non-default? is_non_zero(value: &Self::Value) -> bool90 fn is_non_zero(value: &Self::Value) -> bool; 91 92 /// Write the value. set_from_value_box(target: &mut Self::Value, value_box: ReflectValueBox)93 fn set_from_value_box(target: &mut Self::Value, value_box: ReflectValueBox) { 94 *target = Self::from_value_box(value_box).expect("wrong type"); 95 } 96 97 /// Cast values to enum values. 98 /// 99 /// # Panics 100 /// 101 /// If self is not an enum. cast_to_enum_values(values: &[Self::Value]) -> &[i32]102 fn cast_to_enum_values(values: &[Self::Value]) -> &[i32] { 103 let _ = values; 104 panic!("not enum") 105 } 106 107 /// Parse the value from unknown fields. get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value>108 fn get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value>; 109 } 110 111 /// Runtime type which can be dereferenced. 112 pub trait RuntimeTypeWithDeref: RuntimeTypeTrait { 113 /// Deref target. 114 type DerefTarget: ?Sized; 115 116 /// Deref. 117 // TODO: rename to `deref` deref_as_ref(value: &Self::DerefTarget) -> ReflectValueRef118 fn deref_as_ref(value: &Self::DerefTarget) -> ReflectValueRef; 119 } 120 121 /// Types which can be hashmap keys. 122 pub trait RuntimeTypeHashable: RuntimeTypeTrait { 123 /// Query hash map with a given key. hash_map_get<'a, V>(map: &'a HashMap<Self::Value, V>, key: ReflectValueRef) -> Option<&'a V>124 fn hash_map_get<'a, V>(map: &'a HashMap<Self::Value, V>, key: ReflectValueRef) 125 -> Option<&'a V>; 126 } 127 128 /// Implementation for `f32` 129 #[derive(Debug, Copy, Clone)] 130 pub struct RuntimeTypeF32; 131 /// Implementation for `f64` 132 #[derive(Debug, Copy, Clone)] 133 pub struct RuntimeTypeF64; 134 /// Implementation for `i32` 135 #[derive(Debug, Copy, Clone)] 136 pub struct RuntimeTypeI32; 137 /// Implementation for `f32` 138 #[derive(Debug, Copy, Clone)] 139 pub struct RuntimeTypeI64; 140 /// Implementation for `u32` 141 #[derive(Debug, Copy, Clone)] 142 pub struct RuntimeTypeU32; 143 /// Implementation for `u64` 144 #[derive(Debug, Copy, Clone)] 145 pub struct RuntimeTypeU64; 146 /// Implementation for `bool` 147 #[derive(Debug, Copy, Clone)] 148 pub struct RuntimeTypeBool; 149 /// Implementation for `String` 150 #[derive(Debug, Copy, Clone)] 151 pub struct RuntimeTypeString; 152 /// Implementation for `Vec<u8>` 153 #[derive(Debug, Copy, Clone)] 154 pub struct RuntimeTypeVecU8; 155 156 /// Implementation for [`Bytes`]. 157 #[cfg(feature = "bytes")] 158 #[derive(Debug, Copy, Clone)] 159 pub struct RuntimeTypeTokioBytes; 160 /// Implementation for [`Chars`]. 161 #[cfg(feature = "bytes")] 162 #[derive(Debug, Copy, Clone)] 163 pub struct RuntimeTypeTokioChars; 164 165 /// Implementation for enum. 166 #[derive(Debug, Copy, Clone)] 167 pub struct RuntimeTypeEnumOrUnknown<E: EnumFull>(marker::PhantomData<E>); 168 /// Implementation for [`MessageFull`]. 169 #[derive(Debug, Copy, Clone)] 170 pub struct RuntimeTypeMessage<M: MessageFull>(marker::PhantomData<M>); 171 172 impl RuntimeTypeTrait for RuntimeTypeF32 { 173 type Value = f32; 174 runtime_type_box() -> RuntimeType where Self: Sized,175 fn runtime_type_box() -> RuntimeType 176 where 177 Self: Sized, 178 { 179 RuntimeType::F32 180 } 181 default_value_ref() -> ReflectValueRef<'static>182 fn default_value_ref() -> ReflectValueRef<'static> { 183 ReflectValueRef::F32(0.0) 184 } 185 from_value_box(value_box: ReflectValueBox) -> Result<f32, ReflectValueBox>186 fn from_value_box(value_box: ReflectValueBox) -> Result<f32, ReflectValueBox> { 187 match value_box { 188 ReflectValueBox::F32(v) => Ok(v), 189 b => Err(b), 190 } 191 } into_value_box(value: f32) -> ReflectValueBox192 fn into_value_box(value: f32) -> ReflectValueBox { 193 ReflectValueBox::F32(value) 194 } 195 into_static_value_ref(value: f32) -> ReflectValueRef<'static>196 fn into_static_value_ref(value: f32) -> ReflectValueRef<'static> { 197 ReflectValueRef::F32(value) 198 } as_ref(value: &f32) -> ReflectValueRef199 fn as_ref(value: &f32) -> ReflectValueRef { 200 ReflectValueRef::F32(*value) 201 } 202 as_mut(_value: &mut Self::Value) -> ReflectValueMut203 fn as_mut(_value: &mut Self::Value) -> ReflectValueMut { 204 unimplemented!() 205 } 206 is_non_zero(value: &f32) -> bool207 fn is_non_zero(value: &f32) -> bool { 208 *value != 0.0 209 } 210 get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value>211 fn get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value> { 212 assert_eq!(field_type, Type::TYPE_FLOAT); 213 ProtobufTypeFloat::get_from_unknown(unknown) 214 } 215 } 216 217 impl RuntimeTypeTrait for RuntimeTypeF64 { 218 type Value = f64; 219 default_value_ref() -> ReflectValueRef<'static>220 fn default_value_ref() -> ReflectValueRef<'static> { 221 ReflectValueRef::F64(0.0) 222 } 223 runtime_type_box() -> RuntimeType where Self: Sized,224 fn runtime_type_box() -> RuntimeType 225 where 226 Self: Sized, 227 { 228 RuntimeType::F64 229 } 230 from_value_box(value_box: ReflectValueBox) -> Result<f64, ReflectValueBox>231 fn from_value_box(value_box: ReflectValueBox) -> Result<f64, ReflectValueBox> { 232 match value_box { 233 ReflectValueBox::F64(v) => Ok(v), 234 b => Err(b), 235 } 236 } 237 into_value_box(value: f64) -> ReflectValueBox238 fn into_value_box(value: f64) -> ReflectValueBox { 239 ReflectValueBox::F64(value) 240 } 241 into_static_value_ref(value: f64) -> ReflectValueRef<'static>242 fn into_static_value_ref(value: f64) -> ReflectValueRef<'static> { 243 ReflectValueRef::F64(value) 244 } 245 as_ref(value: &f64) -> ReflectValueRef246 fn as_ref(value: &f64) -> ReflectValueRef { 247 ReflectValueRef::F64(*value) 248 } 249 is_non_zero(value: &f64) -> bool250 fn is_non_zero(value: &f64) -> bool { 251 *value != 0.0 252 } 253 as_mut(_value: &mut Self::Value) -> ReflectValueMut254 fn as_mut(_value: &mut Self::Value) -> ReflectValueMut { 255 unimplemented!() 256 } 257 get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value>258 fn get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value> { 259 assert_eq!(field_type, Type::TYPE_DOUBLE); 260 ProtobufTypeDouble::get_from_unknown(unknown) 261 } 262 } 263 264 impl RuntimeTypeTrait for RuntimeTypeI32 { 265 type Value = i32; 266 default_value_ref() -> ReflectValueRef<'static>267 fn default_value_ref() -> ReflectValueRef<'static> { 268 ReflectValueRef::I32(0) 269 } 270 runtime_type_box() -> RuntimeType where Self: Sized,271 fn runtime_type_box() -> RuntimeType 272 where 273 Self: Sized, 274 { 275 RuntimeType::I32 276 } 277 from_value_box(value_box: ReflectValueBox) -> Result<i32, ReflectValueBox>278 fn from_value_box(value_box: ReflectValueBox) -> Result<i32, ReflectValueBox> { 279 match value_box { 280 ReflectValueBox::I32(v) => Ok(v), 281 b => Err(b), 282 } 283 } 284 into_value_box(value: i32) -> ReflectValueBox285 fn into_value_box(value: i32) -> ReflectValueBox { 286 ReflectValueBox::I32(value) 287 } 288 into_static_value_ref(value: i32) -> ReflectValueRef<'static>289 fn into_static_value_ref(value: i32) -> ReflectValueRef<'static> { 290 ReflectValueRef::I32(value) 291 } 292 as_ref(value: &i32) -> ReflectValueRef293 fn as_ref(value: &i32) -> ReflectValueRef { 294 ReflectValueRef::I32(*value) 295 } 296 is_non_zero(value: &i32) -> bool297 fn is_non_zero(value: &i32) -> bool { 298 *value != 0 299 } 300 as_mut(_value: &mut Self::Value) -> ReflectValueMut301 fn as_mut(_value: &mut Self::Value) -> ReflectValueMut { 302 unimplemented!() 303 } 304 get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value>305 fn get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value> { 306 match field_type { 307 Type::TYPE_INT32 => ProtobufTypeInt32::get_from_unknown(unknown), 308 Type::TYPE_SINT32 => ProtobufTypeSint32::get_from_unknown(unknown), 309 Type::TYPE_SFIXED32 => ProtobufTypeSfixed32::get_from_unknown(unknown), 310 _ => panic!("wrong type: {:?}", field_type), 311 } 312 } 313 } 314 impl RuntimeTypeHashable for RuntimeTypeI32 { hash_map_get<'a, V>(map: &'a HashMap<i32, V>, key: ReflectValueRef) -> Option<&'a V>315 fn hash_map_get<'a, V>(map: &'a HashMap<i32, V>, key: ReflectValueRef) -> Option<&'a V> { 316 match key { 317 ReflectValueRef::I32(i) => map.get(&i), 318 _ => None, 319 } 320 } 321 } 322 323 impl RuntimeTypeTrait for RuntimeTypeI64 { 324 type Value = i64; 325 default_value_ref() -> ReflectValueRef<'static>326 fn default_value_ref() -> ReflectValueRef<'static> { 327 ReflectValueRef::I64(0) 328 } 329 runtime_type_box() -> RuntimeType where Self: Sized,330 fn runtime_type_box() -> RuntimeType 331 where 332 Self: Sized, 333 { 334 RuntimeType::I64 335 } 336 from_value_box(value_box: ReflectValueBox) -> Result<i64, ReflectValueBox>337 fn from_value_box(value_box: ReflectValueBox) -> Result<i64, ReflectValueBox> { 338 match value_box { 339 ReflectValueBox::I64(v) => Ok(v), 340 b => Err(b), 341 } 342 } 343 into_value_box(value: i64) -> ReflectValueBox344 fn into_value_box(value: i64) -> ReflectValueBox { 345 ReflectValueBox::I64(value) 346 } 347 into_static_value_ref(value: i64) -> ReflectValueRef<'static>348 fn into_static_value_ref(value: i64) -> ReflectValueRef<'static> { 349 ReflectValueRef::I64(value) 350 } 351 as_ref(value: &i64) -> ReflectValueRef352 fn as_ref(value: &i64) -> ReflectValueRef { 353 ReflectValueRef::I64(*value) 354 } 355 is_non_zero(value: &i64) -> bool356 fn is_non_zero(value: &i64) -> bool { 357 *value != 0 358 } 359 as_mut(_value: &mut Self::Value) -> ReflectValueMut360 fn as_mut(_value: &mut Self::Value) -> ReflectValueMut { 361 unimplemented!() 362 } 363 get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value>364 fn get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value> { 365 match field_type { 366 Type::TYPE_INT64 => ProtobufTypeInt64::get_from_unknown(unknown), 367 Type::TYPE_SINT64 => ProtobufTypeSint64::get_from_unknown(unknown), 368 Type::TYPE_SFIXED64 => ProtobufTypeSfixed64::get_from_unknown(unknown), 369 _ => panic!("wrong type: {:?}", field_type), 370 } 371 } 372 } 373 impl RuntimeTypeHashable for RuntimeTypeI64 { hash_map_get<'a, V>(map: &'a HashMap<i64, V>, key: ReflectValueRef) -> Option<&'a V>374 fn hash_map_get<'a, V>(map: &'a HashMap<i64, V>, key: ReflectValueRef) -> Option<&'a V> { 375 match key { 376 ReflectValueRef::I64(i) => map.get(&i), 377 _ => None, 378 } 379 } 380 } 381 382 impl RuntimeTypeTrait for RuntimeTypeU32 { 383 type Value = u32; 384 runtime_type_box() -> RuntimeType where Self: Sized,385 fn runtime_type_box() -> RuntimeType 386 where 387 Self: Sized, 388 { 389 RuntimeType::U32 390 } 391 default_value_ref() -> ReflectValueRef<'static>392 fn default_value_ref() -> ReflectValueRef<'static> { 393 ReflectValueRef::U32(0) 394 } 395 from_value_box(value_box: ReflectValueBox) -> Result<u32, ReflectValueBox>396 fn from_value_box(value_box: ReflectValueBox) -> Result<u32, ReflectValueBox> { 397 match value_box { 398 ReflectValueBox::U32(v) => Ok(v), 399 b => Err(b), 400 } 401 } 402 into_value_box(value: u32) -> ReflectValueBox403 fn into_value_box(value: u32) -> ReflectValueBox { 404 ReflectValueBox::U32(value) 405 } 406 into_static_value_ref(value: u32) -> ReflectValueRef<'static>407 fn into_static_value_ref(value: u32) -> ReflectValueRef<'static> { 408 ReflectValueRef::U32(value) 409 } 410 as_ref(value: &u32) -> ReflectValueRef411 fn as_ref(value: &u32) -> ReflectValueRef { 412 ReflectValueRef::U32(*value) 413 } 414 as_mut(_value: &mut Self::Value) -> ReflectValueMut415 fn as_mut(_value: &mut Self::Value) -> ReflectValueMut { 416 unimplemented!() 417 } 418 is_non_zero(value: &u32) -> bool419 fn is_non_zero(value: &u32) -> bool { 420 *value != 0 421 } 422 get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value>423 fn get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value> { 424 match field_type { 425 Type::TYPE_UINT32 => ProtobufTypeUint32::get_from_unknown(unknown), 426 Type::TYPE_FIXED32 => ProtobufTypeFixed32::get_from_unknown(unknown), 427 _ => panic!("wrong type: {:?}", field_type), 428 } 429 } 430 } 431 impl RuntimeTypeHashable for RuntimeTypeU32 { hash_map_get<'a, V>(map: &'a HashMap<u32, V>, key: ReflectValueRef) -> Option<&'a V>432 fn hash_map_get<'a, V>(map: &'a HashMap<u32, V>, key: ReflectValueRef) -> Option<&'a V> { 433 match key { 434 ReflectValueRef::U32(i) => map.get(&i), 435 _ => None, 436 } 437 } 438 } 439 440 impl RuntimeTypeTrait for RuntimeTypeU64 { 441 type Value = u64; 442 default_value_ref() -> ReflectValueRef<'static>443 fn default_value_ref() -> ReflectValueRef<'static> { 444 ReflectValueRef::U64(0) 445 } 446 runtime_type_box() -> RuntimeType where Self: Sized,447 fn runtime_type_box() -> RuntimeType 448 where 449 Self: Sized, 450 { 451 RuntimeType::U64 452 } 453 from_value_box(value_box: ReflectValueBox) -> Result<u64, ReflectValueBox>454 fn from_value_box(value_box: ReflectValueBox) -> Result<u64, ReflectValueBox> { 455 match value_box { 456 ReflectValueBox::U64(v) => Ok(v), 457 b => Err(b), 458 } 459 } 460 into_value_box(value: u64) -> ReflectValueBox461 fn into_value_box(value: u64) -> ReflectValueBox { 462 ReflectValueBox::U64(value) 463 } 464 into_static_value_ref(value: u64) -> ReflectValueRef<'static>465 fn into_static_value_ref(value: u64) -> ReflectValueRef<'static> { 466 ReflectValueRef::U64(value) 467 } 468 as_ref(value: &u64) -> ReflectValueRef469 fn as_ref(value: &u64) -> ReflectValueRef { 470 ReflectValueRef::U64(*value) 471 } 472 is_non_zero(value: &u64) -> bool473 fn is_non_zero(value: &u64) -> bool { 474 *value != 0 475 } 476 as_mut(_value: &mut Self::Value) -> ReflectValueMut477 fn as_mut(_value: &mut Self::Value) -> ReflectValueMut { 478 unimplemented!() 479 } 480 get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value>481 fn get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value> { 482 match field_type { 483 Type::TYPE_UINT64 => ProtobufTypeUint64::get_from_unknown(unknown), 484 Type::TYPE_FIXED64 => ProtobufTypeFixed64::get_from_unknown(unknown), 485 _ => panic!("wrong type: {:?}", field_type), 486 } 487 } 488 } 489 impl RuntimeTypeHashable for RuntimeTypeU64 { hash_map_get<'a, V>(map: &'a HashMap<u64, V>, key: ReflectValueRef) -> Option<&'a V>490 fn hash_map_get<'a, V>(map: &'a HashMap<u64, V>, key: ReflectValueRef) -> Option<&'a V> { 491 match key { 492 ReflectValueRef::U64(i) => map.get(&i), 493 _ => None, 494 } 495 } 496 } 497 498 impl RuntimeTypeTrait for RuntimeTypeBool { 499 type Value = bool; 500 default_value_ref() -> ReflectValueRef<'static>501 fn default_value_ref() -> ReflectValueRef<'static> { 502 ReflectValueRef::Bool(false) 503 } 504 runtime_type_box() -> RuntimeType where Self: Sized,505 fn runtime_type_box() -> RuntimeType 506 where 507 Self: Sized, 508 { 509 RuntimeType::Bool 510 } 511 from_value_box(value_box: ReflectValueBox) -> Result<bool, ReflectValueBox>512 fn from_value_box(value_box: ReflectValueBox) -> Result<bool, ReflectValueBox> { 513 match value_box { 514 ReflectValueBox::Bool(v) => Ok(v), 515 b => Err(b), 516 } 517 } 518 into_value_box(value: bool) -> ReflectValueBox519 fn into_value_box(value: bool) -> ReflectValueBox { 520 ReflectValueBox::Bool(value) 521 } 522 into_static_value_ref(value: bool) -> ReflectValueRef<'static>523 fn into_static_value_ref(value: bool) -> ReflectValueRef<'static> { 524 ReflectValueRef::Bool(value) 525 } 526 as_ref(value: &bool) -> ReflectValueRef527 fn as_ref(value: &bool) -> ReflectValueRef { 528 ReflectValueRef::Bool(*value) 529 } 530 is_non_zero(value: &bool) -> bool531 fn is_non_zero(value: &bool) -> bool { 532 *value 533 } 534 as_mut(_value: &mut Self::Value) -> ReflectValueMut535 fn as_mut(_value: &mut Self::Value) -> ReflectValueMut { 536 unimplemented!() 537 } 538 get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value>539 fn get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value> { 540 assert_eq!(field_type, Type::TYPE_BOOL); 541 ProtobufTypeBool::get_from_unknown(unknown) 542 } 543 } 544 impl RuntimeTypeHashable for RuntimeTypeBool { hash_map_get<'a, V>(map: &'a HashMap<bool, V>, key: ReflectValueRef) -> Option<&'a V>545 fn hash_map_get<'a, V>(map: &'a HashMap<bool, V>, key: ReflectValueRef) -> Option<&'a V> { 546 match key { 547 ReflectValueRef::Bool(i) => map.get(&i), 548 _ => None, 549 } 550 } 551 } 552 553 impl RuntimeTypeTrait for RuntimeTypeString { 554 type Value = String; 555 runtime_type_box() -> RuntimeType where Self: Sized,556 fn runtime_type_box() -> RuntimeType 557 where 558 Self: Sized, 559 { 560 RuntimeType::String 561 } 562 default_value_ref() -> ReflectValueRef<'static>563 fn default_value_ref() -> ReflectValueRef<'static> { 564 ReflectValueRef::String("") 565 } 566 from_value_box(value_box: ReflectValueBox) -> Result<String, ReflectValueBox>567 fn from_value_box(value_box: ReflectValueBox) -> Result<String, ReflectValueBox> { 568 match value_box { 569 ReflectValueBox::String(v) => Ok(v), 570 b => Err(b), 571 } 572 } 573 into_value_box(value: String) -> ReflectValueBox574 fn into_value_box(value: String) -> ReflectValueBox { 575 ReflectValueBox::String(value) 576 } 577 as_ref(value: &String) -> ReflectValueRef578 fn as_ref(value: &String) -> ReflectValueRef { 579 ReflectValueRef::String(&*value) 580 } 581 as_mut(_value: &mut Self::Value) -> ReflectValueMut582 fn as_mut(_value: &mut Self::Value) -> ReflectValueMut { 583 unimplemented!() 584 } 585 is_non_zero(value: &String) -> bool586 fn is_non_zero(value: &String) -> bool { 587 !value.is_empty() 588 } 589 get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value>590 fn get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value> { 591 assert_eq!(field_type, Type::TYPE_STRING); 592 ProtobufTypeString::get_from_unknown(unknown) 593 } 594 } 595 impl RuntimeTypeWithDeref for RuntimeTypeString { 596 type DerefTarget = str; 597 deref_as_ref(value: &str) -> ReflectValueRef598 fn deref_as_ref(value: &str) -> ReflectValueRef { 599 ReflectValueRef::String(value) 600 } 601 } 602 impl RuntimeTypeHashable for RuntimeTypeString { hash_map_get<'a, V>(map: &'a HashMap<String, V>, key: ReflectValueRef) -> Option<&'a V>603 fn hash_map_get<'a, V>(map: &'a HashMap<String, V>, key: ReflectValueRef) -> Option<&'a V> { 604 match key { 605 ReflectValueRef::String(s) => map.get(*&s), 606 _ => None, 607 } 608 } 609 } 610 611 impl RuntimeTypeTrait for RuntimeTypeVecU8 { 612 type Value = Vec<u8>; 613 runtime_type_box() -> RuntimeType where Self: Sized,614 fn runtime_type_box() -> RuntimeType 615 where 616 Self: Sized, 617 { 618 RuntimeType::VecU8 619 } 620 default_value_ref() -> ReflectValueRef<'static>621 fn default_value_ref() -> ReflectValueRef<'static> { 622 ReflectValueRef::Bytes(b"") 623 } 624 from_value_box(value_box: ReflectValueBox) -> Result<Vec<u8>, ReflectValueBox>625 fn from_value_box(value_box: ReflectValueBox) -> Result<Vec<u8>, ReflectValueBox> { 626 match value_box { 627 ReflectValueBox::Bytes(v) => Ok(v), 628 b => Err(b), 629 } 630 } 631 into_value_box(value: Vec<u8>) -> ReflectValueBox632 fn into_value_box(value: Vec<u8>) -> ReflectValueBox { 633 ReflectValueBox::Bytes(value) 634 } 635 as_ref(value: &Vec<u8>) -> ReflectValueRef636 fn as_ref(value: &Vec<u8>) -> ReflectValueRef { 637 ReflectValueRef::Bytes(value.as_slice()) 638 } 639 as_mut(_value: &mut Self::Value) -> ReflectValueMut640 fn as_mut(_value: &mut Self::Value) -> ReflectValueMut { 641 unimplemented!() 642 } 643 is_non_zero(value: &Vec<u8>) -> bool644 fn is_non_zero(value: &Vec<u8>) -> bool { 645 !value.is_empty() 646 } 647 get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value>648 fn get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value> { 649 assert_eq!(field_type, Type::TYPE_BYTES); 650 ProtobufTypeBytes::get_from_unknown(unknown) 651 } 652 } 653 impl RuntimeTypeWithDeref for RuntimeTypeVecU8 { 654 type DerefTarget = [u8]; 655 deref_as_ref(value: &[u8]) -> ReflectValueRef656 fn deref_as_ref(value: &[u8]) -> ReflectValueRef { 657 ReflectValueRef::Bytes(value) 658 } 659 } 660 661 #[cfg(feature = "bytes")] 662 impl RuntimeTypeTrait for RuntimeTypeTokioBytes { 663 type Value = Bytes; 664 default_value_ref() -> ReflectValueRef<'static>665 fn default_value_ref() -> ReflectValueRef<'static> { 666 ReflectValueRef::Bytes(b"") 667 } 668 runtime_type_box() -> RuntimeType where Self: Sized,669 fn runtime_type_box() -> RuntimeType 670 where 671 Self: Sized, 672 { 673 RuntimeType::VecU8 674 } 675 from_value_box(value_box: ReflectValueBox) -> Result<Bytes, ReflectValueBox>676 fn from_value_box(value_box: ReflectValueBox) -> Result<Bytes, ReflectValueBox> { 677 match value_box { 678 ReflectValueBox::Bytes(v) => Ok(v.into()), 679 b => Err(b), 680 } 681 } 682 into_value_box(value: Bytes) -> ReflectValueBox683 fn into_value_box(value: Bytes) -> ReflectValueBox { 684 // TODO: copies here 685 ReflectValueBox::Bytes(value.as_ref().to_owned()) 686 } 687 as_ref(value: &Bytes) -> ReflectValueRef688 fn as_ref(value: &Bytes) -> ReflectValueRef { 689 ReflectValueRef::Bytes(value.as_ref()) 690 } 691 is_non_zero(value: &Bytes) -> bool692 fn is_non_zero(value: &Bytes) -> bool { 693 !value.is_empty() 694 } 695 as_mut(_value: &mut Self::Value) -> ReflectValueMut696 fn as_mut(_value: &mut Self::Value) -> ReflectValueMut { 697 unimplemented!() 698 } 699 get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value>700 fn get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value> { 701 assert_eq!(field_type, Type::TYPE_BYTES); 702 ProtobufTypeTokioBytes::get_from_unknown(unknown) 703 } 704 } 705 #[cfg(feature = "bytes")] 706 impl RuntimeTypeWithDeref for RuntimeTypeTokioBytes { 707 type DerefTarget = [u8]; 708 deref_as_ref(value: &[u8]) -> ReflectValueRef709 fn deref_as_ref(value: &[u8]) -> ReflectValueRef { 710 ReflectValueRef::Bytes(value) 711 } 712 } 713 714 #[cfg(feature = "bytes")] 715 impl RuntimeTypeTrait for RuntimeTypeTokioChars { 716 type Value = Chars; 717 default_value_ref() -> ReflectValueRef<'static>718 fn default_value_ref() -> ReflectValueRef<'static> { 719 ReflectValueRef::String("") 720 } 721 runtime_type_box() -> RuntimeType where Self: Sized,722 fn runtime_type_box() -> RuntimeType 723 where 724 Self: Sized, 725 { 726 RuntimeType::String 727 } 728 from_value_box(value_box: ReflectValueBox) -> Result<Chars, ReflectValueBox>729 fn from_value_box(value_box: ReflectValueBox) -> Result<Chars, ReflectValueBox> { 730 match value_box { 731 ReflectValueBox::String(v) => Ok(v.into()), 732 b => Err(b), 733 } 734 } 735 into_value_box(value: Chars) -> ReflectValueBox736 fn into_value_box(value: Chars) -> ReflectValueBox { 737 ReflectValueBox::String(value.into()) 738 } 739 as_ref(value: &Chars) -> ReflectValueRef740 fn as_ref(value: &Chars) -> ReflectValueRef { 741 ReflectValueRef::String(value.as_ref()) 742 } 743 is_non_zero(value: &Chars) -> bool744 fn is_non_zero(value: &Chars) -> bool { 745 !value.is_empty() 746 } 747 as_mut(_value: &mut Self::Value) -> ReflectValueMut748 fn as_mut(_value: &mut Self::Value) -> ReflectValueMut { 749 unimplemented!() 750 } 751 get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value>752 fn get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value> { 753 assert_eq!(field_type, Type::TYPE_STRING); 754 ProtobufTypeTokioChars::get_from_unknown(unknown) 755 } 756 } 757 #[cfg(feature = "bytes")] 758 impl RuntimeTypeWithDeref for RuntimeTypeTokioChars { 759 type DerefTarget = str; 760 deref_as_ref(value: &str) -> ReflectValueRef761 fn deref_as_ref(value: &str) -> ReflectValueRef { 762 ReflectValueRef::String(value) 763 } 764 } 765 #[cfg(feature = "bytes")] 766 impl RuntimeTypeHashable for RuntimeTypeTokioChars { hash_map_get<'a, V>(map: &'a HashMap<Chars, V>, key: ReflectValueRef) -> Option<&'a V>767 fn hash_map_get<'a, V>(map: &'a HashMap<Chars, V>, key: ReflectValueRef) -> Option<&'a V> { 768 match key { 769 ReflectValueRef::String(s) => map.get(&*s), 770 _ => None, 771 } 772 } 773 } 774 775 impl<E> RuntimeTypeTrait for RuntimeTypeEnumOrUnknown<E> 776 where 777 E: EnumFull + fmt::Debug, 778 { 779 type Value = EnumOrUnknown<E>; 780 runtime_type_box() -> RuntimeType where Self: Sized,781 fn runtime_type_box() -> RuntimeType 782 where 783 Self: Sized, 784 { 785 RuntimeType::Enum(E::enum_descriptor()) 786 } 787 default_value_ref() -> ReflectValueRef<'static>788 fn default_value_ref() -> ReflectValueRef<'static> { 789 ReflectValueRef::from(E::enum_descriptor().default_value()) 790 } 791 from_value_box(value_box: ReflectValueBox) -> Result<EnumOrUnknown<E>, ReflectValueBox>792 fn from_value_box(value_box: ReflectValueBox) -> Result<EnumOrUnknown<E>, ReflectValueBox> { 793 match value_box { 794 ReflectValueBox::Enum(d, v) if d == E::enum_descriptor() => { 795 Ok(EnumOrUnknown::from_i32(v)) 796 } 797 b => Err(b), 798 } 799 } 800 into_value_box(value: EnumOrUnknown<E>) -> ReflectValueBox801 fn into_value_box(value: EnumOrUnknown<E>) -> ReflectValueBox { 802 ReflectValueBox::Enum(E::enum_descriptor(), value.value()) 803 } 804 into_static_value_ref(value: EnumOrUnknown<E>) -> ReflectValueRef<'static>805 fn into_static_value_ref(value: EnumOrUnknown<E>) -> ReflectValueRef<'static> { 806 ReflectValueRef::Enum(E::enum_descriptor(), value.value()) 807 } 808 as_ref(value: &EnumOrUnknown<E>) -> ReflectValueRef809 fn as_ref(value: &EnumOrUnknown<E>) -> ReflectValueRef { 810 ReflectValueRef::Enum(E::enum_descriptor(), value.value()) 811 } 812 as_mut(_value: &mut Self::Value) -> ReflectValueMut813 fn as_mut(_value: &mut Self::Value) -> ReflectValueMut { 814 unimplemented!() 815 } 816 is_non_zero(value: &EnumOrUnknown<E>) -> bool817 fn is_non_zero(value: &EnumOrUnknown<E>) -> bool { 818 value.value() != 0 819 } 820 cast_to_enum_values(values: &[EnumOrUnknown<E>]) -> &[i32]821 fn cast_to_enum_values(values: &[EnumOrUnknown<E>]) -> &[i32] { 822 EnumOrUnknown::cast_to_values(values) 823 } 824 get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value>825 fn get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value> { 826 assert_eq!(field_type, Type::TYPE_ENUM); 827 ProtobufTypeEnumOrUnknown::<E>::get_from_unknown(unknown) 828 } 829 } 830 831 impl<M> RuntimeTypeTrait for RuntimeTypeMessage<M> 832 where 833 M: MessageFull + ProtobufValue + Clone + Default, 834 { 835 type Value = M; 836 runtime_type_box() -> RuntimeType where Self: Sized,837 fn runtime_type_box() -> RuntimeType 838 where 839 Self: Sized, 840 { 841 RuntimeType::Message(M::descriptor()) 842 } 843 default_value_ref() -> ReflectValueRef<'static>844 fn default_value_ref() -> ReflectValueRef<'static> { 845 ReflectValueRef::Message(MessageRef::new(M::default_instance())) 846 } 847 from_value_box(value_box: ReflectValueBox) -> Result<M, ReflectValueBox>848 fn from_value_box(value_box: ReflectValueBox) -> Result<M, ReflectValueBox> { 849 match value_box { 850 ReflectValueBox::Message(v) => v 851 .downcast_box() 852 .map(|v| *v) 853 .map_err(ReflectValueBox::Message), 854 b => Err(b), 855 } 856 } 857 into_value_box(value: M) -> ReflectValueBox858 fn into_value_box(value: M) -> ReflectValueBox { 859 ReflectValueBox::Message(Box::new(value)) 860 } as_ref(value: &M) -> ReflectValueRef861 fn as_ref(value: &M) -> ReflectValueRef { 862 ReflectValueRef::Message(MessageRef::new(value)) 863 } 864 as_mut(value: &mut M) -> ReflectValueMut865 fn as_mut(value: &mut M) -> ReflectValueMut { 866 ReflectValueMut::Message(value) 867 } 868 is_non_zero(_value: &M) -> bool869 fn is_non_zero(_value: &M) -> bool { 870 true 871 } 872 get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value>873 fn get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value> { 874 assert_eq!(field_type, Type::TYPE_MESSAGE); 875 ProtobufTypeMessage::<M>::get_from_unknown(unknown) 876 } 877 } 878