<NME> runtime_types.rs
<BEF> //! Implementations of `RuntimeType` for all types.

use std::collections::HashMap;
use std::fmt;
use std::marker;

#[cfg(feature = "bytes")]
use bytes::Bytes;

#[cfg(feature = "bytes")]
use crate::chars::Chars;
use crate::descriptor::field_descriptor_proto::Type;
use crate::enum_or_unknown::EnumOrUnknown;
use crate::message_full::MessageFull;
use crate::reflect::runtime_type_box::RuntimeType;
use crate::reflect::types::ProtobufTypeBool;
use crate::reflect::types::ProtobufTypeBytes;
use crate::reflect::types::ProtobufTypeDouble;
use crate::reflect::types::ProtobufTypeEnumOrUnknown;
use crate::reflect::types::ProtobufTypeFixed32;
use crate::reflect::types::ProtobufTypeFixed64;
use crate::reflect::types::ProtobufTypeFloat;
use crate::reflect::types::ProtobufTypeInt32;
use crate::reflect::types::ProtobufTypeInt64;
use crate::reflect::types::ProtobufTypeMessage;
use crate::reflect::types::ProtobufTypeSfixed32;
use crate::reflect::types::ProtobufTypeSfixed64;
use crate::reflect::types::ProtobufTypeSint32;
use crate::reflect::types::ProtobufTypeSint64;
use crate::reflect::types::ProtobufTypeString;
#[cfg(feature = "bytes")]
use crate::reflect::types::ProtobufTypeTokioBytes;
#[cfg(feature = "bytes")]
use crate::reflect::types::ProtobufTypeTokioChars;
use crate::reflect::types::ProtobufTypeTrait;
use crate::reflect::types::ProtobufTypeUint32;
use crate::reflect::types::ProtobufTypeUint64;
use crate::reflect::value::value_ref::ReflectValueMut;
use crate::reflect::MessageRef;
use crate::reflect::ProtobufValue;
use crate::reflect::ReflectValueBox;
use crate::reflect::ReflectValueRef;
use crate::EnumFull;
use crate::UnknownValueRef;

/// `RuntimeType` is not implemented by all protobuf types directly
/// because it's not possible to implement `RuntimeType` for all `Message`
/// implementations at once: each `Message` implementation has to reimplement
/// all the methods again. With current strategy there's only implementation
/// for all messages, which is `RuntimeTypeMessage`.
///
/// The downside is that we have to explicitly specify type parameters
/// in a lot of places.
pub trait RuntimeTypeTrait: fmt::Debug + Send + Sync + Sized + 'static {
    /// Actual value for this type.
    type Value: ProtobufValue + Clone + Sized + fmt::Debug + Default;

    /// "Box" version of type type.
    fn runtime_type_box() -> RuntimeType;

    /// Default value for this type.
    fn default_value_ref() -> ReflectValueRef<'static>;

    /// Construct a value from given reflective value.
    ///
    /// # Panics
    ///
    /// If reflective value is of incompatible type.
    fn from_value_box(value_box: ReflectValueBox) -> Result<Self::Value, ReflectValueBox>;

    /// Convert a value into a refletive box value.
    fn into_value_box(value: Self::Value) -> ReflectValueBox;

    /// Convert a value into a ref value if possible.
    ///
    /// # Panics
    ///
    /// For message and enum.
    // TODO: move the operation into a separate trait
    fn into_static_value_ref(value: Self::Value) -> ReflectValueRef<'static> {
        panic!("value {:?} cannot be converted to static ref", value)
    }

    /// Pointer to a dynamic reference.
    fn as_ref(value: &Self::Value) -> ReflectValueRef;
    /// Mutable pointer to a dynamic mutable reference.
    fn as_mut(value: &mut Self::Value) -> ReflectValueMut;

    /// Value is non-default?
    fn is_non_zero(value: &Self::Value) -> bool;

    /// Write the value.
    fn set_from_value_box(target: &mut Self::Value, value_box: ReflectValueBox) {
        *target = Self::from_value_box(value_box).expect("wrong type");
    }

    /// Cast values to enum values.
    ///
    /// # Panics
    ///
    /// If self is not an enum.
    fn cast_to_enum_values(values: &[Self::Value]) -> &[i32] {
        let _ = values;
        panic!("not enum")
    }

    /// Parse the value from unknown fields.
    fn get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value>;
}

/// Runtime type which can be dereferenced.
pub trait RuntimeTypeWithDeref: RuntimeTypeTrait {
    /// Deref target.
    type DerefTarget: ?Sized;

    /// Deref.
    // TODO: rename to `deref`
    fn deref_as_ref(value: &Self::DerefTarget) -> ReflectValueRef;
}

/// Types which can be hashmap keys.
pub trait RuntimeTypeHashable: RuntimeTypeTrait {
    /// Query hash map with a given key.
    fn hash_map_get<'a, V>(map: &'a HashMap<Self::Value, V>, key: ReflectValueRef)
        -> Option<&'a V>;
}

/// Implementation for `f32`
#[derive(Debug, Copy, Clone)]
pub struct RuntimeTypeF32;
/// Implementation for `f64`
#[derive(Debug, Copy, Clone)]
pub struct RuntimeTypeF64;
/// Implementation for `i32`
#[derive(Debug, Copy, Clone)]
pub struct RuntimeTypeI32;
/// Implementation for `f32`
#[derive(Debug, Copy, Clone)]
pub struct RuntimeTypeI64;
/// Implementation for `u32`
#[derive(Debug, Copy, Clone)]
pub struct RuntimeTypeU32;
/// Implementation for `u64`
#[derive(Debug, Copy, Clone)]
pub struct RuntimeTypeU64;
/// Implementation for `bool`
#[derive(Debug, Copy, Clone)]
pub struct RuntimeTypeBool;
/// Implementation for `String`
#[derive(Debug, Copy, Clone)]
pub struct RuntimeTypeString;
/// Implementation for `Vec<u8>`
#[derive(Debug, Copy, Clone)]
pub struct RuntimeTypeVecU8;

/// Implementation for [`Bytes`].
#[cfg(feature = "bytes")]
#[derive(Debug, Copy, Clone)]
pub struct RuntimeTypeTokioBytes;
/// Implementation for [`Chars`].
#[cfg(feature = "bytes")]
#[derive(Debug, Copy, Clone)]
pub struct RuntimeTypeTokioChars;

/// Implementation for enum.
#[derive(Debug, Copy, Clone)]
pub struct RuntimeTypeEnumOrUnknown<E: EnumFull>(marker::PhantomData<E>);
/// Implementation for [`MessageFull`].
#[derive(Debug, Copy, Clone)]
pub struct RuntimeTypeMessage<M: MessageFull>(marker::PhantomData<M>);

impl RuntimeTypeTrait for RuntimeTypeF32 {
    type Value = f32;

    fn runtime_type_box() -> RuntimeType
    where
        Self: Sized,
    {
        RuntimeType::F32
    }

    fn default_value_ref() -> ReflectValueRef<'static> {
        ReflectValueRef::F32(0.0)
    }

    fn from_value_box(value_box: ReflectValueBox) -> Result<f32, ReflectValueBox> {
        match value_box {
            ReflectValueBox::F32(v) => Ok(v),
            b => Err(b),
        }
    }
    fn into_value_box(value: f32) -> ReflectValueBox {
        ReflectValueBox::F32(value)
    }

    fn into_static_value_ref(value: f32) -> ReflectValueRef<'static> {
        ReflectValueRef::F32(value)
    }
    fn as_ref(value: &f32) -> ReflectValueRef {
        ReflectValueRef::F32(*value)
    }

    fn as_mut(_value: &mut Self::Value) -> ReflectValueMut {
        unimplemented!()
    }

    fn is_non_zero(value: &f32) -> bool {
        *value != 0.0
    }

    fn get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value> {
        assert_eq!(field_type, Type::TYPE_FLOAT);
        ProtobufTypeFloat::get_from_unknown(unknown)
    }
}

impl RuntimeTypeTrait for RuntimeTypeF64 {
    type Value = f64;

    fn default_value_ref() -> ReflectValueRef<'static> {
        ReflectValueRef::F64(0.0)
    }

    fn runtime_type_box() -> RuntimeType
    where
        Self: Sized,
    {
        RuntimeType::F64
    }

    fn from_value_box(value_box: ReflectValueBox) -> Result<f64, ReflectValueBox> {
        match value_box {
            ReflectValueBox::F64(v) => Ok(v),
            b => Err(b),
        }
    }

    fn into_value_box(value: f64) -> ReflectValueBox {
        ReflectValueBox::F64(value)
    }

    fn into_static_value_ref(value: f64) -> ReflectValueRef<'static> {
        ReflectValueRef::F64(value)
    }

    fn as_ref(value: &f64) -> ReflectValueRef {
        ReflectValueRef::F64(*value)
    }

    fn is_non_zero(value: &f64) -> bool {
        *value != 0.0
    }

    fn as_mut(_value: &mut Self::Value) -> ReflectValueMut {
        unimplemented!()
    }

    fn get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value> {
        assert_eq!(field_type, Type::TYPE_DOUBLE);
        ProtobufTypeDouble::get_from_unknown(unknown)
    }
}

impl RuntimeTypeTrait for RuntimeTypeI32 {
    type Value = i32;

    fn default_value_ref() -> ReflectValueRef<'static> {
        ReflectValueRef::I32(0)
    }

    fn runtime_type_box() -> RuntimeType
    where
        Self: Sized,
    {
        RuntimeType::I32
    }

    fn from_value_box(value_box: ReflectValueBox) -> Result<i32, ReflectValueBox> {
        match value_box {
            ReflectValueBox::I32(v) => Ok(v),
            b => Err(b),
        }
    }

    fn into_value_box(value: i32) -> ReflectValueBox {
        ReflectValueBox::I32(value)
    }

    fn into_static_value_ref(value: i32) -> ReflectValueRef<'static> {
        ReflectValueRef::I32(value)
    }

    fn as_ref(value: &i32) -> ReflectValueRef {
        ReflectValueRef::I32(*value)
    }

    fn is_non_zero(value: &i32) -> bool {
        *value != 0
    }

    fn as_mut(_value: &mut Self::Value) -> ReflectValueMut {
        unimplemented!()
    }

    fn get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value> {
        match field_type {
            Type::TYPE_INT32 => ProtobufTypeInt32::get_from_unknown(unknown),
            Type::TYPE_SINT32 => ProtobufTypeSint32::get_from_unknown(unknown),
            Type::TYPE_SFIXED32 => ProtobufTypeSfixed32::get_from_unknown(unknown),
            _ => panic!("wrong type: {:?}", field_type),
        }
    }
}
impl RuntimeTypeHashable for RuntimeTypeI32 {
    fn hash_map_get<'a, V>(map: &'a HashMap<i32, V>, key: ReflectValueRef) -> Option<&'a V> {
        match key {
            ReflectValueRef::I32(i) => map.get(&i),
            _ => None,
        }
    }
}

impl RuntimeTypeTrait for RuntimeTypeI64 {
    type Value = i64;

    fn default_value_ref() -> ReflectValueRef<'static> {
        ReflectValueRef::I64(0)
    }

    fn runtime_type_box() -> RuntimeType
    where
        Self: Sized,
    {
        RuntimeType::I64
    }

    fn from_value_box(value_box: ReflectValueBox) -> Result<i64, ReflectValueBox> {
        match value_box {
            ReflectValueBox::I64(v) => Ok(v),
            b => Err(b),
        }
    }

    fn into_value_box(value: i64) -> ReflectValueBox {
        ReflectValueBox::I64(value)
    }

    fn into_static_value_ref(value: i64) -> ReflectValueRef<'static> {
        ReflectValueRef::I64(value)
    }

    fn as_ref(value: &i64) -> ReflectValueRef {
        ReflectValueRef::I64(*value)
    }

    fn is_non_zero(value: &i64) -> bool {
        *value != 0
    }

    fn as_mut(_value: &mut Self::Value) -> ReflectValueMut {
        unimplemented!()
    }

    fn get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value> {
        match field_type {
            Type::TYPE_INT64 => ProtobufTypeInt64::get_from_unknown(unknown),
            Type::TYPE_SINT64 => ProtobufTypeSint64::get_from_unknown(unknown),
            Type::TYPE_SFIXED64 => ProtobufTypeSfixed64::get_from_unknown(unknown),
            _ => panic!("wrong type: {:?}", field_type),
        }
    }
}
impl RuntimeTypeHashable for RuntimeTypeI64 {
    fn hash_map_get<'a, V>(map: &'a HashMap<i64, V>, key: ReflectValueRef) -> Option<&'a V> {
        match key {
            ReflectValueRef::I64(i) => map.get(&i),
            _ => None,
        }
    }
}

impl RuntimeTypeTrait for RuntimeTypeU32 {
    type Value = u32;

    fn runtime_type_box() -> RuntimeType
    where
        Self: Sized,
    {
        RuntimeType::U32
    }

    fn default_value_ref() -> ReflectValueRef<'static> {
        ReflectValueRef::U32(0)
    }

    fn from_value_box(value_box: ReflectValueBox) -> Result<u32, ReflectValueBox> {
        match value_box {
            ReflectValueBox::U32(v) => Ok(v),
            b => Err(b),
        }
    }

    fn into_value_box(value: u32) -> ReflectValueBox {
        ReflectValueBox::U32(value)
    }

    fn into_static_value_ref(value: u32) -> ReflectValueRef<'static> {
        ReflectValueRef::U32(value)
    }

    fn as_ref(value: &u32) -> ReflectValueRef {
        ReflectValueRef::U32(*value)
    }

    fn as_mut(_value: &mut Self::Value) -> ReflectValueMut {
        unimplemented!()
    }

    fn is_non_zero(value: &u32) -> bool {
        *value != 0
    }

    fn get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value> {
        match field_type {
            Type::TYPE_UINT32 => ProtobufTypeUint32::get_from_unknown(unknown),
            Type::TYPE_FIXED32 => ProtobufTypeFixed32::get_from_unknown(unknown),
            _ => panic!("wrong type: {:?}", field_type),
        }
    }
}
impl RuntimeTypeHashable for RuntimeTypeU32 {
    fn hash_map_get<'a, V>(map: &'a HashMap<u32, V>, key: ReflectValueRef) -> Option<&'a V> {
        match key {
            ReflectValueRef::U32(i) => map.get(&i),
            _ => None,
        }
    }
}

impl RuntimeTypeTrait for RuntimeTypeU64 {
    type Value = u64;

    fn default_value_ref() -> ReflectValueRef<'static> {
        ReflectValueRef::U64(0)
    }

    fn runtime_type_box() -> RuntimeType
    where
        Self: Sized,
    {
        RuntimeType::U64
    }

    fn from_value_box(value_box: ReflectValueBox) -> Result<u64, ReflectValueBox> {
        match value_box {
            ReflectValueBox::U64(v) => Ok(v),
            b => Err(b),
        }
    }

    fn into_value_box(value: u64) -> ReflectValueBox {
        ReflectValueBox::U64(value)
    }

    fn into_static_value_ref(value: u64) -> ReflectValueRef<'static> {
        ReflectValueRef::U64(value)
    }

    fn as_ref(value: &u64) -> ReflectValueRef {
        ReflectValueRef::U64(*value)
    }

    fn is_non_zero(value: &u64) -> bool {
        *value != 0
    }

    fn as_mut(_value: &mut Self::Value) -> ReflectValueMut {
        unimplemented!()
    }

    fn get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value> {
        match field_type {
            Type::TYPE_UINT64 => ProtobufTypeUint64::get_from_unknown(unknown),
            Type::TYPE_FIXED64 => ProtobufTypeFixed64::get_from_unknown(unknown),
            _ => panic!("wrong type: {:?}", field_type),
        }
    }
}
impl RuntimeTypeHashable for RuntimeTypeU64 {
    fn hash_map_get<'a, V>(map: &'a HashMap<u64, V>, key: ReflectValueRef) -> Option<&'a V> {
        match key {
            ReflectValueRef::U64(i) => map.get(&i),
            _ => None,
        }
    }
}

impl RuntimeTypeTrait for RuntimeTypeBool {
    type Value = bool;

    fn default_value_ref() -> ReflectValueRef<'static> {
        ReflectValueRef::Bool(false)
    }

    fn runtime_type_box() -> RuntimeType
    where
        Self: Sized,
    {
        RuntimeType::Bool
    }

    fn from_value_box(value_box: ReflectValueBox) -> Result<bool, ReflectValueBox> {
        match value_box {
            ReflectValueBox::Bool(v) => Ok(v),
            b => Err(b),
        }
    }

    fn into_value_box(value: bool) -> ReflectValueBox {
        ReflectValueBox::Bool(value)
    }

    fn into_static_value_ref(value: bool) -> ReflectValueRef<'static> {
        ReflectValueRef::Bool(value)
    }

    fn as_ref(value: &bool) -> ReflectValueRef {
        ReflectValueRef::Bool(*value)
    }

    fn is_non_zero(value: &bool) -> bool {
        *value
    }

    fn as_mut(_value: &mut Self::Value) -> ReflectValueMut {
        unimplemented!()
    }

    fn get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value> {
        assert_eq!(field_type, Type::TYPE_BOOL);
        ProtobufTypeBool::get_from_unknown(unknown)
    }
}
impl RuntimeTypeHashable for RuntimeTypeBool {
    fn hash_map_get<'a, V>(map: &'a HashMap<bool, V>, key: ReflectValueRef) -> Option<&'a V> {
        match key {
            ReflectValueRef::Bool(i) => map.get(&i),
            _ => None,
        }
    }
}

impl RuntimeTypeTrait for RuntimeTypeString {
    type Value = String;

    fn runtime_type_box() -> RuntimeType
    where
        Self: Sized,
    {
        RuntimeType::String
    }

    fn default_value_ref() -> ReflectValueRef<'static> {
        ReflectValueRef::String("")
    }

    fn from_value_box(value_box: ReflectValueBox) -> Result<String, ReflectValueBox> {
        match value_box {
            ReflectValueBox::String(v) => Ok(v),
            b => Err(b),
        }
    }

    fn into_value_box(value: String) -> ReflectValueBox {
        ReflectValueBox::String(value)
    }

    fn as_ref(value: &String) -> ReflectValueRef {
        ReflectValueRef::String(&*value)
    }

    fn as_mut(_value: &mut Self::Value) -> ReflectValueMut {
        unimplemented!()
    }

    fn is_non_zero(value: &String) -> bool {
        !value.is_empty()
    }

    fn get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value> {
        assert_eq!(field_type, Type::TYPE_STRING);
        ProtobufTypeString::get_from_unknown(unknown)
    }
}
impl RuntimeTypeWithDeref for RuntimeTypeString {
    type DerefTarget = str;

    fn deref_as_ref(value: &str) -> ReflectValueRef {
        ReflectValueRef::String(value)
    }
}
impl RuntimeTypeHashable for RuntimeTypeString {
    fn hash_map_get<'a, V>(map: &'a HashMap<String, V>, key: ReflectValueRef) -> Option<&'a V> {
        match key {
            ReflectValueRef::String(s) => map.get(*&s),
            _ => None,
        }
    }
}

impl RuntimeTypeTrait for RuntimeTypeVecU8 {
    type Value = Vec<u8>;

    fn runtime_type_box() -> RuntimeType
    where
    fn from_value_box(value_box: ReflectValueBox) -> M {
        match value_box {
            ReflectValueBox::Message(v) => {
                *v.into_any_box().downcast().expect("wrong message type")
            }
            _ => panic!("wrong type"),
        }
    }

    fn from_value_box(value_box: ReflectValueBox) -> Result<Vec<u8>, ReflectValueBox> {
        match value_box {
            ReflectValueBox::Bytes(v) => Ok(v),
            b => Err(b),
        }
    }

    fn into_value_box(value: Vec<u8>) -> ReflectValueBox {
        ReflectValueBox::Bytes(value)
    }

    fn as_ref(value: &Vec<u8>) -> ReflectValueRef {
        ReflectValueRef::Bytes(value.as_slice())
    }

    fn as_mut(_value: &mut Self::Value) -> ReflectValueMut {
        unimplemented!()
    }

    fn is_non_zero(value: &Vec<u8>) -> bool {
        !value.is_empty()
    }

    fn get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value> {
        assert_eq!(field_type, Type::TYPE_BYTES);
        ProtobufTypeBytes::get_from_unknown(unknown)
    }
}
impl RuntimeTypeWithDeref for RuntimeTypeVecU8 {
    type DerefTarget = [u8];

    fn deref_as_ref(value: &[u8]) -> ReflectValueRef {
        ReflectValueRef::Bytes(value)
    }
}

#[cfg(feature = "bytes")]
impl RuntimeTypeTrait for RuntimeTypeTokioBytes {
    type Value = Bytes;

    fn default_value_ref() -> ReflectValueRef<'static> {
        ReflectValueRef::Bytes(b"")
    }

    fn runtime_type_box() -> RuntimeType
    where
        Self: Sized,
    {
        RuntimeType::VecU8
    }

    fn from_value_box(value_box: ReflectValueBox) -> Result<Bytes, ReflectValueBox> {
        match value_box {
            ReflectValueBox::Bytes(v) => Ok(v.into()),
            b => Err(b),
        }
    }

    fn into_value_box(value: Bytes) -> ReflectValueBox {
        // TODO: copies here
        ReflectValueBox::Bytes(value.as_ref().to_owned())
    }

    fn as_ref(value: &Bytes) -> ReflectValueRef {
        ReflectValueRef::Bytes(value.as_ref())
    }

    fn is_non_zero(value: &Bytes) -> bool {
        !value.is_empty()
    }

    fn as_mut(_value: &mut Self::Value) -> ReflectValueMut {
        unimplemented!()
    }

    fn get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value> {
        assert_eq!(field_type, Type::TYPE_BYTES);
        ProtobufTypeTokioBytes::get_from_unknown(unknown)
    }
}
#[cfg(feature = "bytes")]
impl RuntimeTypeWithDeref for RuntimeTypeTokioBytes {
    type DerefTarget = [u8];

    fn deref_as_ref(value: &[u8]) -> ReflectValueRef {
        ReflectValueRef::Bytes(value)
    }
}

#[cfg(feature = "bytes")]
impl RuntimeTypeTrait for RuntimeTypeTokioChars {
    type Value = Chars;

    fn default_value_ref() -> ReflectValueRef<'static> {
        ReflectValueRef::String("")
    }

    fn runtime_type_box() -> RuntimeType
    where
        Self: Sized,
    {
        RuntimeType::String
    }

    fn from_value_box(value_box: ReflectValueBox) -> Result<Chars, ReflectValueBox> {
        match value_box {
            ReflectValueBox::String(v) => Ok(v.into()),
            b => Err(b),
        }
    }

    fn into_value_box(value: Chars) -> ReflectValueBox {
        ReflectValueBox::String(value.into())
    }

    fn as_ref(value: &Chars) -> ReflectValueRef {
        ReflectValueRef::String(value.as_ref())
    }

    fn is_non_zero(value: &Chars) -> bool {
        !value.is_empty()
    }

    fn as_mut(_value: &mut Self::Value) -> ReflectValueMut {
        unimplemented!()
    }

    fn get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value> {
        assert_eq!(field_type, Type::TYPE_STRING);
        ProtobufTypeTokioChars::get_from_unknown(unknown)
    }
}
#[cfg(feature = "bytes")]
impl RuntimeTypeWithDeref for RuntimeTypeTokioChars {
    type DerefTarget = str;

    fn deref_as_ref(value: &str) -> ReflectValueRef {
        ReflectValueRef::String(value)
    }
}
#[cfg(feature = "bytes")]
impl RuntimeTypeHashable for RuntimeTypeTokioChars {
    fn hash_map_get<'a, V>(map: &'a HashMap<Chars, V>, key: ReflectValueRef) -> Option<&'a V> {
        match key {
            ReflectValueRef::String(s) => map.get(&*s),
            _ => None,
        }
    }
}

impl<E> RuntimeTypeTrait for RuntimeTypeEnumOrUnknown<E>
where
    E: EnumFull + fmt::Debug,
{
    type Value = EnumOrUnknown<E>;

    fn runtime_type_box() -> RuntimeType
    where
        Self: Sized,
    {
        RuntimeType::Enum(E::enum_descriptor())
    }

    fn default_value_ref() -> ReflectValueRef<'static> {
        ReflectValueRef::from(E::enum_descriptor().default_value())
    }

    fn from_value_box(value_box: ReflectValueBox) -> Result<EnumOrUnknown<E>, ReflectValueBox> {
        match value_box {
            ReflectValueBox::Enum(d, v) if d == E::enum_descriptor() => {
                Ok(EnumOrUnknown::from_i32(v))
            }
            b => Err(b),
        }
    }

    fn into_value_box(value: EnumOrUnknown<E>) -> ReflectValueBox {
        ReflectValueBox::Enum(E::enum_descriptor(), value.value())
    }

    fn into_static_value_ref(value: EnumOrUnknown<E>) -> ReflectValueRef<'static> {
        ReflectValueRef::Enum(E::enum_descriptor(), value.value())
    }

    fn as_ref(value: &EnumOrUnknown<E>) -> ReflectValueRef {
        ReflectValueRef::Enum(E::enum_descriptor(), value.value())
    }

    fn as_mut(_value: &mut Self::Value) -> ReflectValueMut {
        unimplemented!()
    }

    fn is_non_zero(value: &EnumOrUnknown<E>) -> bool {
        value.value() != 0
    }

    fn cast_to_enum_values(values: &[EnumOrUnknown<E>]) -> &[i32] {
        EnumOrUnknown::cast_to_values(values)
    }

    fn get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value> {
        assert_eq!(field_type, Type::TYPE_ENUM);
        ProtobufTypeEnumOrUnknown::<E>::get_from_unknown(unknown)
    }
}

impl<M> RuntimeTypeTrait for RuntimeTypeMessage<M>
where
    M: MessageFull + ProtobufValue + Clone + Default,
{
    type Value = M;

    fn runtime_type_box() -> RuntimeType
    where
        Self: Sized,
    {
        RuntimeType::Message(M::descriptor())
    }

    fn default_value_ref() -> ReflectValueRef<'static> {
        ReflectValueRef::Message(MessageRef::new(M::default_instance()))
    }

    fn from_value_box(value_box: ReflectValueBox) -> Result<M, ReflectValueBox> {
        match value_box {
            ReflectValueBox::Message(v) => v
                .downcast_box()
                .map(|v| *v)
                .map_err(ReflectValueBox::Message),
            b => Err(b),
        }
    }

    fn into_value_box(value: M) -> ReflectValueBox {
        ReflectValueBox::Message(Box::new(value))
    }
    fn as_ref(value: &M) -> ReflectValueRef {
        ReflectValueRef::Message(MessageRef::new(value))
    }

    fn as_mut(value: &mut M) -> ReflectValueMut {
        ReflectValueMut::Message(value)
    }

    fn is_non_zero(_value: &M) -> bool {
        true
    }

    fn get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value> {
        assert_eq!(field_type, Type::TYPE_MESSAGE);
        ProtobufTypeMessage::<M>::get_from_unknown(unknown)
    }
}

<MSG> Remove into_any_box op from AsAny

This slightly reduces generated machine code size

<DFF> @@ -616,7 +616,7 @@ where
     fn from_value_box(value_box: ReflectValueBox) -> M {
         match value_box {
             ReflectValueBox::Message(v) => {
-                *v.into_any_box().downcast().expect("wrong message type")
+                *v.downcast_box().expect("wrong message type")
             }
             _ => panic!("wrong type"),
         }

{'commit': 'diff --git a/protobuf/src/reflect/as_any.rs b/protobuf/src/reflect/as_any.rs\nindex 6f509699..27bd761f 100644\n--- a/protobuf/src/reflect/as_any.rs\n+++ b/protobuf/src/reflect/as_any.rs\n@@ -8,7 +8,6 @@ use std::any::Any;\n pub trait AsAny {\n     fn as_any(&self) -> &Any;\n     fn as_any_mut(&mut self) -> &mut Any;\n-    fn into_any_box(self: Box<Self>) -> Box<Any>;\n }\n \n impl<T: \'static> AsAny for T {\n@@ -19,8 +18,4 @@ impl<T: \'static> AsAny for T {\n     fn as_any_mut(&mut self) -> &mut Any {\n         self\n     }\n-\n-    fn into_any_box(self: Box<Self>) -> Box<Any> {\n-        Box::new(*self)\n-    }\n }\ndiff --git a/protobuf/src/reflect/runtime_types.rs b/protobuf/src/reflect/runtime_types.rs\nindex f0a91f2f..edfdef37 100644\n--- a/protobuf/src/reflect/runtime_types.rs\n+++ b/protobuf/src/reflect/runtime_types.rs\n@@ -616,7 +616,7 @@ where\n     fn from_value_box(value_box: ReflectValueBox) -> M {\n         match value_box {\n             ReflectValueBox::Message(v) => {\n-                *v.into_any_box().downcast().expect("wrong message type")\n+                *v.downcast_box().expect("wrong message type")\n             }\n             _ => panic!("wrong type"),\n         }\n', 'commit_subject': 'Remove into_any_box op from AsAny', 'repo_name': 'stepancheg/rust-protobuf', 'committer': {'name': 'Stepan Koltsov', 'email': 'e150b500682c3c55bd53434656d52cd8d6051776@gmail.com', 'time_sec': '1549159883', 'tz_offset': '0', 'date': {'seconds': '1549159883'}}, 'commit_tree': 'e5832f2e6d7bfba15594105e96576b2a1ddd1c94', 'commit_message': 'Remove into_any_box op from AsAny\n\nThis slightly reduces generated machine code size\n', 'language_name': 'Rust', 'license': 'mit', 'hunks': '@@ -616,7 +616,7 @@ where\n     fn from_value_box(value_box: ReflectValueBox) -> M {\n         match value_box {\n             ReflectValueBox::Message(v) => {\n-                *v.into_any_box().downcast().expect("wrong message type")\n+                *v.downcast_box().expect("wrong message type")\n             }\n             _ => panic!("wrong type"),\n         }\n', 'addition_count': 1, 'deletion_count': 1, 'src_file': 'a/protobuf/src/reflect/runtime_types.rs', 'tgt_file': 'b/protobuf/src/reflect/runtime_types.rs', 'file_extension': '.rs', 'before_file': '//! Implementations of `RuntimeType` for all types.\n\nuse std::collections::HashMap;\nuse std::fmt;\nuse std::marker;\n\n#[cfg(feature = "bytes")]\nuse bytes::Bytes;\n\n#[cfg(feature = "bytes")]\nuse crate::chars::Chars;\nuse crate::descriptor::field_descriptor_proto::Type;\nuse crate::enum_or_unknown::EnumOrUnknown;\nuse crate::message_full::MessageFull;\nuse crate::reflect::runtime_type_box::RuntimeType;\nuse crate::reflect::types::ProtobufTypeBool;\nuse crate::reflect::types::ProtobufTypeBytes;\nuse crate::reflect::types::ProtobufTypeDouble;\nuse crate::reflect::types::ProtobufTypeEnumOrUnknown;\nuse crate::reflect::types::ProtobufTypeFixed32;\nuse crate::reflect::types::ProtobufTypeFixed64;\nuse crate::reflect::types::ProtobufTypeFloat;\nuse crate::reflect::types::ProtobufTypeInt32;\nuse crate::reflect::types::ProtobufTypeInt64;\nuse crate::reflect::types::ProtobufTypeMessage;\nuse crate::reflect::types::ProtobufTypeSfixed32;\nuse crate::reflect::types::ProtobufTypeSfixed64;\nuse crate::reflect::types::ProtobufTypeSint32;\nuse crate::reflect::types::ProtobufTypeSint64;\nuse crate::reflect::types::ProtobufTypeString;\n#[cfg(feature = "bytes")]\nuse crate::reflect::types::ProtobufTypeTokioBytes;\n#[cfg(feature = "bytes")]\nuse crate::reflect::types::ProtobufTypeTokioChars;\nuse crate::reflect::types::ProtobufTypeTrait;\nuse crate::reflect::types::ProtobufTypeUint32;\nuse crate::reflect::types::ProtobufTypeUint64;\nuse crate::reflect::value::value_ref::ReflectValueMut;\nuse crate::reflect::MessageRef;\nuse crate::reflect::ProtobufValue;\nuse crate::reflect::ReflectValueBox;\nuse crate::reflect::ReflectValueRef;\nuse crate::EnumFull;\nuse crate::UnknownValueRef;\n\n/// `RuntimeType` is not implemented by all protobuf types directly\n/// because it\'s not possible to implement `RuntimeType` for all `Message`\n/// implementations at once: each `Message` implementation has to reimplement\n/// all the methods again. With current strategy there\'s only implementation\n/// for all messages, which is `RuntimeTypeMessage`.\n///\n/// The downside is that we have to explicitly specify type parameters\n/// in a lot of places.\npub trait RuntimeTypeTrait: fmt::Debug + Send + Sync + Sized + \'static {\n    /// Actual value for this type.\n    type Value: ProtobufValue + Clone + Sized + fmt::Debug + Default;\n\n    /// "Box" version of type type.\n    fn runtime_type_box() -> RuntimeType;\n\n    /// Default value for this type.\n    fn default_value_ref() -> ReflectValueRef<\'static>;\n\n    /// Construct a value from given reflective value.\n    ///\n    /// # Panics\n    ///\n    /// If reflective value is of incompatible type.\n    fn from_value_box(value_box: ReflectValueBox) -> Result<Self::Value, ReflectValueBox>;\n\n    /// Convert a value into a refletive box value.\n    fn into_value_box(value: Self::Value) -> ReflectValueBox;\n\n    /// Convert a value into a ref value if possible.\n    ///\n    /// # Panics\n    ///\n    /// For message and enum.\n    // TODO: move the operation into a separate trait\n    fn into_static_value_ref(value: Self::Value) -> ReflectValueRef<\'static> {\n        panic!("value {:?} cannot be converted to static ref", value)\n    }\n\n    /// Pointer to a dynamic reference.\n    fn as_ref(value: &Self::Value) -> ReflectValueRef;\n    /// Mutable pointer to a dynamic mutable reference.\n    fn as_mut(value: &mut Self::Value) -> ReflectValueMut;\n\n    /// Value is non-default?\n    fn is_non_zero(value: &Self::Value) -> bool;\n\n    /// Write the value.\n    fn set_from_value_box(target: &mut Self::Value, value_box: ReflectValueBox) {\n        *target = Self::from_value_box(value_box).expect("wrong type");\n    }\n\n    /// Cast values to enum values.\n    ///\n    /// # Panics\n    ///\n    /// If self is not an enum.\n    fn cast_to_enum_values(values: &[Self::Value]) -> &[i32] {\n        let _ = values;\n        panic!("not enum")\n    }\n\n    /// Parse the value from unknown fields.\n    fn get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value>;\n}\n\n/// Runtime type which can be dereferenced.\npub trait RuntimeTypeWithDeref: RuntimeTypeTrait {\n    /// Deref target.\n    type DerefTarget: ?Sized;\n\n    /// Deref.\n    // TODO: rename to `deref`\n    fn deref_as_ref(value: &Self::DerefTarget) -> ReflectValueRef;\n}\n\n/// Types which can be hashmap keys.\npub trait RuntimeTypeHashable: RuntimeTypeTrait {\n    /// Query hash map with a given key.\n    fn hash_map_get<\'a, V>(map: &\'a HashMap<Self::Value, V>, key: ReflectValueRef)\n        -> Option<&\'a V>;\n}\n\n/// Implementation for `f32`\n#[derive(Debug, Copy, Clone)]\npub struct RuntimeTypeF32;\n/// Implementation for `f64`\n#[derive(Debug, Copy, Clone)]\npub struct RuntimeTypeF64;\n/// Implementation for `i32`\n#[derive(Debug, Copy, Clone)]\npub struct RuntimeTypeI32;\n/// Implementation for `f32`\n#[derive(Debug, Copy, Clone)]\npub struct RuntimeTypeI64;\n/// Implementation for `u32`\n#[derive(Debug, Copy, Clone)]\npub struct RuntimeTypeU32;\n/// Implementation for `u64`\n#[derive(Debug, Copy, Clone)]\npub struct RuntimeTypeU64;\n/// Implementation for `bool`\n#[derive(Debug, Copy, Clone)]\npub struct RuntimeTypeBool;\n/// Implementation for `String`\n#[derive(Debug, Copy, Clone)]\npub struct RuntimeTypeString;\n/// Implementation for `Vec<u8>`\n#[derive(Debug, Copy, Clone)]\npub struct RuntimeTypeVecU8;\n\n/// Implementation for [`Bytes`].\n#[cfg(feature = "bytes")]\n#[derive(Debug, Copy, Clone)]\npub struct RuntimeTypeTokioBytes;\n/// Implementation for [`Chars`].\n#[cfg(feature = "bytes")]\n#[derive(Debug, Copy, Clone)]\npub struct RuntimeTypeTokioChars;\n\n/// Implementation for enum.\n#[derive(Debug, Copy, Clone)]\npub struct RuntimeTypeEnumOrUnknown<E: EnumFull>(marker::PhantomData<E>);\n/// Implementation for [`MessageFull`].\n#[derive(Debug, Copy, Clone)]\npub struct RuntimeTypeMessage<M: MessageFull>(marker::PhantomData<M>);\n\nimpl RuntimeTypeTrait for RuntimeTypeF32 {\n    type Value = f32;\n\n    fn runtime_type_box() -> RuntimeType\n    where\n        Self: Sized,\n    {\n        RuntimeType::F32\n    }\n\n    fn default_value_ref() -> ReflectValueRef<\'static> {\n        ReflectValueRef::F32(0.0)\n    }\n\n    fn from_value_box(value_box: ReflectValueBox) -> Result<f32, ReflectValueBox> {\n        match value_box {\n            ReflectValueBox::F32(v) => Ok(v),\n            b => Err(b),\n        }\n    }\n    fn into_value_box(value: f32) -> ReflectValueBox {\n        ReflectValueBox::F32(value)\n    }\n\n    fn into_static_value_ref(value: f32) -> ReflectValueRef<\'static> {\n        ReflectValueRef::F32(value)\n    }\n    fn as_ref(value: &f32) -> ReflectValueRef {\n        ReflectValueRef::F32(*value)\n    }\n\n    fn as_mut(_value: &mut Self::Value) -> ReflectValueMut {\n        unimplemented!()\n    }\n\n    fn is_non_zero(value: &f32) -> bool {\n        *value != 0.0\n    }\n\n    fn get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value> {\n        assert_eq!(field_type, Type::TYPE_FLOAT);\n        ProtobufTypeFloat::get_from_unknown(unknown)\n    }\n}\n\nimpl RuntimeTypeTrait for RuntimeTypeF64 {\n    type Value = f64;\n\n    fn default_value_ref() -> ReflectValueRef<\'static> {\n        ReflectValueRef::F64(0.0)\n    }\n\n    fn runtime_type_box() -> RuntimeType\n    where\n        Self: Sized,\n    {\n        RuntimeType::F64\n    }\n\n    fn from_value_box(value_box: ReflectValueBox) -> Result<f64, ReflectValueBox> {\n        match value_box {\n            ReflectValueBox::F64(v) => Ok(v),\n            b => Err(b),\n        }\n    }\n\n    fn into_value_box(value: f64) -> ReflectValueBox {\n        ReflectValueBox::F64(value)\n    }\n\n    fn into_static_value_ref(value: f64) -> ReflectValueRef<\'static> {\n        ReflectValueRef::F64(value)\n    }\n\n    fn as_ref(value: &f64) -> ReflectValueRef {\n        ReflectValueRef::F64(*value)\n    }\n\n    fn is_non_zero(value: &f64) -> bool {\n        *value != 0.0\n    }\n\n    fn as_mut(_value: &mut Self::Value) -> ReflectValueMut {\n        unimplemented!()\n    }\n\n    fn get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value> {\n        assert_eq!(field_type, Type::TYPE_DOUBLE);\n        ProtobufTypeDouble::get_from_unknown(unknown)\n    }\n}\n\nimpl RuntimeTypeTrait for RuntimeTypeI32 {\n    type Value = i32;\n\n    fn default_value_ref() -> ReflectValueRef<\'static> {\n        ReflectValueRef::I32(0)\n    }\n\n    fn runtime_type_box() -> RuntimeType\n    where\n        Self: Sized,\n    {\n        RuntimeType::I32\n    }\n\n    fn from_value_box(value_box: ReflectValueBox) -> Result<i32, ReflectValueBox> {\n        match value_box {\n            ReflectValueBox::I32(v) => Ok(v),\n            b => Err(b),\n        }\n    }\n\n    fn into_value_box(value: i32) -> ReflectValueBox {\n        ReflectValueBox::I32(value)\n    }\n\n    fn into_static_value_ref(value: i32) -> ReflectValueRef<\'static> {\n        ReflectValueRef::I32(value)\n    }\n\n    fn as_ref(value: &i32) -> ReflectValueRef {\n        ReflectValueRef::I32(*value)\n    }\n\n    fn is_non_zero(value: &i32) -> bool {\n        *value != 0\n    }\n\n    fn as_mut(_value: &mut Self::Value) -> ReflectValueMut {\n        unimplemented!()\n    }\n\n    fn get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value> {\n        match field_type {\n            Type::TYPE_INT32 => ProtobufTypeInt32::get_from_unknown(unknown),\n            Type::TYPE_SINT32 => ProtobufTypeSint32::get_from_unknown(unknown),\n            Type::TYPE_SFIXED32 => ProtobufTypeSfixed32::get_from_unknown(unknown),\n            _ => panic!("wrong type: {:?}", field_type),\n        }\n    }\n}\nimpl RuntimeTypeHashable for RuntimeTypeI32 {\n    fn hash_map_get<\'a, V>(map: &\'a HashMap<i32, V>, key: ReflectValueRef) -> Option<&\'a V> {\n        match key {\n            ReflectValueRef::I32(i) => map.get(&i),\n            _ => None,\n        }\n    }\n}\n\nimpl RuntimeTypeTrait for RuntimeTypeI64 {\n    type Value = i64;\n\n    fn default_value_ref() -> ReflectValueRef<\'static> {\n        ReflectValueRef::I64(0)\n    }\n\n    fn runtime_type_box() -> RuntimeType\n    where\n        Self: Sized,\n    {\n        RuntimeType::I64\n    }\n\n    fn from_value_box(value_box: ReflectValueBox) -> Result<i64, ReflectValueBox> {\n        match value_box {\n            ReflectValueBox::I64(v) => Ok(v),\n            b => Err(b),\n        }\n    }\n\n    fn into_value_box(value: i64) -> ReflectValueBox {\n        ReflectValueBox::I64(value)\n    }\n\n    fn into_static_value_ref(value: i64) -> ReflectValueRef<\'static> {\n        ReflectValueRef::I64(value)\n    }\n\n    fn as_ref(value: &i64) -> ReflectValueRef {\n        ReflectValueRef::I64(*value)\n    }\n\n    fn is_non_zero(value: &i64) -> bool {\n        *value != 0\n    }\n\n    fn as_mut(_value: &mut Self::Value) -> ReflectValueMut {\n        unimplemented!()\n    }\n\n    fn get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value> {\n        match field_type {\n            Type::TYPE_INT64 => ProtobufTypeInt64::get_from_unknown(unknown),\n            Type::TYPE_SINT64 => ProtobufTypeSint64::get_from_unknown(unknown),\n            Type::TYPE_SFIXED64 => ProtobufTypeSfixed64::get_from_unknown(unknown),\n            _ => panic!("wrong type: {:?}", field_type),\n        }\n    }\n}\nimpl RuntimeTypeHashable for RuntimeTypeI64 {\n    fn hash_map_get<\'a, V>(map: &\'a HashMap<i64, V>, key: ReflectValueRef) -> Option<&\'a V> {\n        match key {\n            ReflectValueRef::I64(i) => map.get(&i),\n            _ => None,\n        }\n    }\n}\n\nimpl RuntimeTypeTrait for RuntimeTypeU32 {\n    type Value = u32;\n\n    fn runtime_type_box() -> RuntimeType\n    where\n        Self: Sized,\n    {\n        RuntimeType::U32\n    }\n\n    fn default_value_ref() -> ReflectValueRef<\'static> {\n        ReflectValueRef::U32(0)\n    }\n\n    fn from_value_box(value_box: ReflectValueBox) -> Result<u32, ReflectValueBox> {\n        match value_box {\n            ReflectValueBox::U32(v) => Ok(v),\n            b => Err(b),\n        }\n    }\n\n    fn into_value_box(value: u32) -> ReflectValueBox {\n        ReflectValueBox::U32(value)\n    }\n\n    fn into_static_value_ref(value: u32) -> ReflectValueRef<\'static> {\n        ReflectValueRef::U32(value)\n    }\n\n    fn as_ref(value: &u32) -> ReflectValueRef {\n        ReflectValueRef::U32(*value)\n    }\n\n    fn as_mut(_value: &mut Self::Value) -> ReflectValueMut {\n        unimplemented!()\n    }\n\n    fn is_non_zero(value: &u32) -> bool {\n        *value != 0\n    }\n\n    fn get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value> {\n        match field_type {\n            Type::TYPE_UINT32 => ProtobufTypeUint32::get_from_unknown(unknown),\n            Type::TYPE_FIXED32 => ProtobufTypeFixed32::get_from_unknown(unknown),\n            _ => panic!("wrong type: {:?}", field_type),\n        }\n    }\n}\nimpl RuntimeTypeHashable for RuntimeTypeU32 {\n    fn hash_map_get<\'a, V>(map: &\'a HashMap<u32, V>, key: ReflectValueRef) -> Option<&\'a V> {\n        match key {\n            ReflectValueRef::U32(i) => map.get(&i),\n            _ => None,\n        }\n    }\n}\n\nimpl RuntimeTypeTrait for RuntimeTypeU64 {\n    type Value = u64;\n\n    fn default_value_ref() -> ReflectValueRef<\'static> {\n        ReflectValueRef::U64(0)\n    }\n\n    fn runtime_type_box() -> RuntimeType\n    where\n        Self: Sized,\n    {\n        RuntimeType::U64\n    }\n\n    fn from_value_box(value_box: ReflectValueBox) -> Result<u64, ReflectValueBox> {\n        match value_box {\n            ReflectValueBox::U64(v) => Ok(v),\n            b => Err(b),\n        }\n    }\n\n    fn into_value_box(value: u64) -> ReflectValueBox {\n        ReflectValueBox::U64(value)\n    }\n\n    fn into_static_value_ref(value: u64) -> ReflectValueRef<\'static> {\n        ReflectValueRef::U64(value)\n    }\n\n    fn as_ref(value: &u64) -> ReflectValueRef {\n        ReflectValueRef::U64(*value)\n    }\n\n    fn is_non_zero(value: &u64) -> bool {\n        *value != 0\n    }\n\n    fn as_mut(_value: &mut Self::Value) -> ReflectValueMut {\n        unimplemented!()\n    }\n\n    fn get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value> {\n        match field_type {\n            Type::TYPE_UINT64 => ProtobufTypeUint64::get_from_unknown(unknown),\n            Type::TYPE_FIXED64 => ProtobufTypeFixed64::get_from_unknown(unknown),\n            _ => panic!("wrong type: {:?}", field_type),\n        }\n    }\n}\nimpl RuntimeTypeHashable for RuntimeTypeU64 {\n    fn hash_map_get<\'a, V>(map: &\'a HashMap<u64, V>, key: ReflectValueRef) -> Option<&\'a V> {\n        match key {\n            ReflectValueRef::U64(i) => map.get(&i),\n            _ => None,\n        }\n    }\n}\n\nimpl RuntimeTypeTrait for RuntimeTypeBool {\n    type Value = bool;\n\n    fn default_value_ref() -> ReflectValueRef<\'static> {\n        ReflectValueRef::Bool(false)\n    }\n\n    fn runtime_type_box() -> RuntimeType\n    where\n        Self: Sized,\n    {\n        RuntimeType::Bool\n    }\n\n    fn from_value_box(value_box: ReflectValueBox) -> Result<bool, ReflectValueBox> {\n        match value_box {\n            ReflectValueBox::Bool(v) => Ok(v),\n            b => Err(b),\n        }\n    }\n\n    fn into_value_box(value: bool) -> ReflectValueBox {\n        ReflectValueBox::Bool(value)\n    }\n\n    fn into_static_value_ref(value: bool) -> ReflectValueRef<\'static> {\n        ReflectValueRef::Bool(value)\n    }\n\n    fn as_ref(value: &bool) -> ReflectValueRef {\n        ReflectValueRef::Bool(*value)\n    }\n\n    fn is_non_zero(value: &bool) -> bool {\n        *value\n    }\n\n    fn as_mut(_value: &mut Self::Value) -> ReflectValueMut {\n        unimplemented!()\n    }\n\n    fn get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value> {\n        assert_eq!(field_type, Type::TYPE_BOOL);\n        ProtobufTypeBool::get_from_unknown(unknown)\n    }\n}\nimpl RuntimeTypeHashable for RuntimeTypeBool {\n    fn hash_map_get<\'a, V>(map: &\'a HashMap<bool, V>, key: ReflectValueRef) -> Option<&\'a V> {\n        match key {\n            ReflectValueRef::Bool(i) => map.get(&i),\n            _ => None,\n        }\n    }\n}\n\nimpl RuntimeTypeTrait for RuntimeTypeString {\n    type Value = String;\n\n    fn runtime_type_box() -> RuntimeType\n    where\n        Self: Sized,\n    {\n        RuntimeType::String\n    }\n\n    fn default_value_ref() -> ReflectValueRef<\'static> {\n        ReflectValueRef::String("")\n    }\n\n    fn from_value_box(value_box: ReflectValueBox) -> Result<String, ReflectValueBox> {\n        match value_box {\n            ReflectValueBox::String(v) => Ok(v),\n            b => Err(b),\n        }\n    }\n\n    fn into_value_box(value: String) -> ReflectValueBox {\n        ReflectValueBox::String(value)\n    }\n\n    fn as_ref(value: &String) -> ReflectValueRef {\n        ReflectValueRef::String(&*value)\n    }\n\n    fn as_mut(_value: &mut Self::Value) -> ReflectValueMut {\n        unimplemented!()\n    }\n\n    fn is_non_zero(value: &String) -> bool {\n        !value.is_empty()\n    }\n\n    fn get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value> {\n        assert_eq!(field_type, Type::TYPE_STRING);\n        ProtobufTypeString::get_from_unknown(unknown)\n    }\n}\nimpl RuntimeTypeWithDeref for RuntimeTypeString {\n    type DerefTarget = str;\n\n    fn deref_as_ref(value: &str) -> ReflectValueRef {\n        ReflectValueRef::String(value)\n    }\n}\nimpl RuntimeTypeHashable for RuntimeTypeString {\n    fn hash_map_get<\'a, V>(map: &\'a HashMap<String, V>, key: ReflectValueRef) -> Option<&\'a V> {\n        match key {\n            ReflectValueRef::String(s) => map.get(*&s),\n            _ => None,\n        }\n    }\n}\n\nimpl RuntimeTypeTrait for RuntimeTypeVecU8 {\n    type Value = Vec<u8>;\n\n    fn runtime_type_box() -> RuntimeType\n    where\n    fn from_value_box(value_box: ReflectValueBox) -> M {\n        match value_box {\n            ReflectValueBox::Message(v) => {\n                *v.into_any_box().downcast().expect("wrong message type")\n            }\n            _ => panic!("wrong type"),\n        }\n    }\n\n    fn from_value_box(value_box: ReflectValueBox) -> Result<Vec<u8>, ReflectValueBox> {\n        match value_box {\n            ReflectValueBox::Bytes(v) => Ok(v),\n            b => Err(b),\n        }\n    }\n\n    fn into_value_box(value: Vec<u8>) -> ReflectValueBox {\n        ReflectValueBox::Bytes(value)\n    }\n\n    fn as_ref(value: &Vec<u8>) -> ReflectValueRef {\n        ReflectValueRef::Bytes(value.as_slice())\n    }\n\n    fn as_mut(_value: &mut Self::Value) -> ReflectValueMut {\n        unimplemented!()\n    }\n\n    fn is_non_zero(value: &Vec<u8>) -> bool {\n        !value.is_empty()\n    }\n\n    fn get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value> {\n        assert_eq!(field_type, Type::TYPE_BYTES);\n        ProtobufTypeBytes::get_from_unknown(unknown)\n    }\n}\nimpl RuntimeTypeWithDeref for RuntimeTypeVecU8 {\n    type DerefTarget = [u8];\n\n    fn deref_as_ref(value: &[u8]) -> ReflectValueRef {\n        ReflectValueRef::Bytes(value)\n    }\n}\n\n#[cfg(feature = "bytes")]\nimpl RuntimeTypeTrait for RuntimeTypeTokioBytes {\n    type Value = Bytes;\n\n    fn default_value_ref() -> ReflectValueRef<\'static> {\n        ReflectValueRef::Bytes(b"")\n    }\n\n    fn runtime_type_box() -> RuntimeType\n    where\n        Self: Sized,\n    {\n        RuntimeType::VecU8\n    }\n\n    fn from_value_box(value_box: ReflectValueBox) -> Result<Bytes, ReflectValueBox> {\n        match value_box {\n            ReflectValueBox::Bytes(v) => Ok(v.into()),\n            b => Err(b),\n        }\n    }\n\n    fn into_value_box(value: Bytes) -> ReflectValueBox {\n        // TODO: copies here\n        ReflectValueBox::Bytes(value.as_ref().to_owned())\n    }\n\n    fn as_ref(value: &Bytes) -> ReflectValueRef {\n        ReflectValueRef::Bytes(value.as_ref())\n    }\n\n    fn is_non_zero(value: &Bytes) -> bool {\n        !value.is_empty()\n    }\n\n    fn as_mut(_value: &mut Self::Value) -> ReflectValueMut {\n        unimplemented!()\n    }\n\n    fn get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value> {\n        assert_eq!(field_type, Type::TYPE_BYTES);\n        ProtobufTypeTokioBytes::get_from_unknown(unknown)\n    }\n}\n#[cfg(feature = "bytes")]\nimpl RuntimeTypeWithDeref for RuntimeTypeTokioBytes {\n    type DerefTarget = [u8];\n\n    fn deref_as_ref(value: &[u8]) -> ReflectValueRef {\n        ReflectValueRef::Bytes(value)\n    }\n}\n\n#[cfg(feature = "bytes")]\nimpl RuntimeTypeTrait for RuntimeTypeTokioChars {\n    type Value = Chars;\n\n    fn default_value_ref() -> ReflectValueRef<\'static> {\n        ReflectValueRef::String("")\n    }\n\n    fn runtime_type_box() -> RuntimeType\n    where\n        Self: Sized,\n    {\n        RuntimeType::String\n    }\n\n    fn from_value_box(value_box: ReflectValueBox) -> Result<Chars, ReflectValueBox> {\n        match value_box {\n            ReflectValueBox::String(v) => Ok(v.into()),\n            b => Err(b),\n        }\n    }\n\n    fn into_value_box(value: Chars) -> ReflectValueBox {\n        ReflectValueBox::String(value.into())\n    }\n\n    fn as_ref(value: &Chars) -> ReflectValueRef {\n        ReflectValueRef::String(value.as_ref())\n    }\n\n    fn is_non_zero(value: &Chars) -> bool {\n        !value.is_empty()\n    }\n\n    fn as_mut(_value: &mut Self::Value) -> ReflectValueMut {\n        unimplemented!()\n    }\n\n    fn get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value> {\n        assert_eq!(field_type, Type::TYPE_STRING);\n        ProtobufTypeTokioChars::get_from_unknown(unknown)\n    }\n}\n#[cfg(feature = "bytes")]\nimpl RuntimeTypeWithDeref for RuntimeTypeTokioChars {\n    type DerefTarget = str;\n\n    fn deref_as_ref(value: &str) -> ReflectValueRef {\n        ReflectValueRef::String(value)\n    }\n}\n#[cfg(feature = "bytes")]\nimpl RuntimeTypeHashable for RuntimeTypeTokioChars {\n    fn hash_map_get<\'a, V>(map: &\'a HashMap<Chars, V>, key: ReflectValueRef) -> Option<&\'a V> {\n        match key {\n            ReflectValueRef::String(s) => map.get(&*s),\n            _ => None,\n        }\n    }\n}\n\nimpl<E> RuntimeTypeTrait for RuntimeTypeEnumOrUnknown<E>\nwhere\n    E: EnumFull + fmt::Debug,\n{\n    type Value = EnumOrUnknown<E>;\n\n    fn runtime_type_box() -> RuntimeType\n    where\n        Self: Sized,\n    {\n        RuntimeType::Enum(E::enum_descriptor())\n    }\n\n    fn default_value_ref() -> ReflectValueRef<\'static> {\n        ReflectValueRef::from(E::enum_descriptor().default_value())\n    }\n\n    fn from_value_box(value_box: ReflectValueBox) -> Result<EnumOrUnknown<E>, ReflectValueBox> {\n        match value_box {\n            ReflectValueBox::Enum(d, v) if d == E::enum_descriptor() => {\n                Ok(EnumOrUnknown::from_i32(v))\n            }\n            b => Err(b),\n        }\n    }\n\n    fn into_value_box(value: EnumOrUnknown<E>) -> ReflectValueBox {\n        ReflectValueBox::Enum(E::enum_descriptor(), value.value())\n    }\n\n    fn into_static_value_ref(value: EnumOrUnknown<E>) -> ReflectValueRef<\'static> {\n        ReflectValueRef::Enum(E::enum_descriptor(), value.value())\n    }\n\n    fn as_ref(value: &EnumOrUnknown<E>) -> ReflectValueRef {\n        ReflectValueRef::Enum(E::enum_descriptor(), value.value())\n    }\n\n    fn as_mut(_value: &mut Self::Value) -> ReflectValueMut {\n        unimplemented!()\n    }\n\n    fn is_non_zero(value: &EnumOrUnknown<E>) -> bool {\n        value.value() != 0\n    }\n\n    fn cast_to_enum_values(values: &[EnumOrUnknown<E>]) -> &[i32] {\n        EnumOrUnknown::cast_to_values(values)\n    }\n\n    fn get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value> {\n        assert_eq!(field_type, Type::TYPE_ENUM);\n        ProtobufTypeEnumOrUnknown::<E>::get_from_unknown(unknown)\n    }\n}\n\nimpl<M> RuntimeTypeTrait for RuntimeTypeMessage<M>\nwhere\n    M: MessageFull + ProtobufValue + Clone + Default,\n{\n    type Value = M;\n\n    fn runtime_type_box() -> RuntimeType\n    where\n        Self: Sized,\n    {\n        RuntimeType::Message(M::descriptor())\n    }\n\n    fn default_value_ref() -> ReflectValueRef<\'static> {\n        ReflectValueRef::Message(MessageRef::new(M::default_instance()))\n    }\n\n    fn from_value_box(value_box: ReflectValueBox) -> Result<M, ReflectValueBox> {\n        match value_box {\n            ReflectValueBox::Message(v) => v\n                .downcast_box()\n                .map(|v| *v)\n                .map_err(ReflectValueBox::Message),\n            b => Err(b),\n        }\n    }\n\n    fn into_value_box(value: M) -> ReflectValueBox {\n        ReflectValueBox::Message(Box::new(value))\n    }\n    fn as_ref(value: &M) -> ReflectValueRef {\n        ReflectValueRef::Message(MessageRef::new(value))\n    }\n\n    fn as_mut(value: &mut M) -> ReflectValueMut {\n        ReflectValueMut::Message(value)\n    }\n\n    fn is_non_zero(_value: &M) -> bool {\n        true\n    }\n\n    fn get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value> {\n        assert_eq!(field_type, Type::TYPE_MESSAGE);\n        ProtobufTypeMessage::<M>::get_from_unknown(unknown)\n    }\n}\n', 'message': 'Remove into_any_box op from AsAny\n\nThis slightly reduces generated machine code size\n', 'author_name': 'Stepan Koltsov', 'lang': 'rs'}