Skip to content

Commit

Permalink
Remove usage of trapper
Browse files Browse the repository at this point in the history
 * Check later if we need to use another design
  • Loading branch information
ObsidianMinor committed Jan 25, 2020
1 parent 2d1a3c0 commit 42caf59
Show file tree
Hide file tree
Showing 8 changed files with 291 additions and 294 deletions.
1 change: 0 additions & 1 deletion protrust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ std = []
checked_size = []

[dependencies]
trapper = "2.0"
ahash = "0.2"

[dependencies.hashbrown]
Expand Down
19 changes: 9 additions & 10 deletions protrust/src/collections/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use crate::io::{self, read, write, WireType, FieldNumber, Tag, LengthBuilder, Le
use crate::raw::{self, Value, Packable, Packed};
use core::convert::TryInto;
use core::hash::Hash;
use trapper::Wrapper;

pub mod unknown_fields;

Expand Down Expand Up @@ -65,7 +64,7 @@ impl<'a, T: Input> TryRead<'a, T> {
pub type RepeatedField<T> = alloc::vec::Vec<T>;

impl<T> Sealed for RepeatedField<T> { }
impl<V: Value + Wrapper> RepeatedValue<V> for RepeatedField<V::Inner> {
impl<V: Value> RepeatedValue<V> for RepeatedField<V::Inner> {
#[inline]
fn add_entries_from<T: Input>(&mut self, input: &mut CodedReader<T>) -> read::Result<()> {
input.read_value::<V>().map(|v| self.push(v))
Expand Down Expand Up @@ -102,10 +101,10 @@ impl<V: Value + Wrapper> RepeatedValue<V> for RepeatedField<V::Inner> {
Ok(())
}
fn is_initialized(&self) -> bool {
self.iter().map(V::wrap_ref).all(V::is_initialized)
self.iter().all(V::is_initialized)
}
}
impl<V: Value + Packable + Wrapper> RepeatedValue<Packed<V>> for RepeatedField<V::Inner> {
impl<V: Value + Packable> RepeatedValue<Packed<V>> for RepeatedField<V::Inner> {
#[inline]
fn add_entries_from<T: Input>(&mut self, input: &mut CodedReader<T>) -> read::Result<()> {
input.read_limit()?.for_all(|input| input.read_value::<V>().map(|v| self.push(v)))
Expand Down Expand Up @@ -142,7 +141,7 @@ impl<V: Value + Packable + Wrapper> RepeatedValue<Packed<V>> for RepeatedField<V
Ok(())
}
fn is_initialized(&self) -> bool {
self.iter().map(V::wrap_ref).all(V::is_initialized)
self.iter().all(V::is_initialized)
}
}
impl<V: Clone> Mergable for RepeatedField<V> {
Expand All @@ -161,9 +160,9 @@ const VALUE_FIELD: FieldNumber = unsafe { FieldNumber::new_unchecked(2) };
impl<K, V> Sealed for MapField<K, V> { }
impl<K, V> RepeatedValue<(K, V)> for MapField<K::Inner, V::Inner>
where
K: Value + Wrapper,
K: Value,
K::Inner: Default + Eq + Hash,
V: Value + Wrapper,
V: Value,
V::Inner: Default
{
fn add_entries_from<T: Input>(&mut self, input: &mut CodedReader<T>) -> read::Result<()> {
Expand Down Expand Up @@ -236,7 +235,7 @@ impl<K, V> RepeatedValue<(K, V)> for MapField<K::Inner, V::Inner>
Ok(())
}
fn is_initialized(&self) -> bool {
self.values().map(V::wrap_ref).all(V::is_initialized)
self.values().all(V::is_initialized)
}
}

Expand All @@ -260,7 +259,7 @@ trait ValuesSize<T> {
}

impl<V> ValuesSize<V> for RepeatedField<V::Inner>
where V: Value + Wrapper
where V: Value
{
default fn calculate_size(&self, mut builder: LengthBuilder) -> Option<LengthBuilder> {
for value in self {
Expand All @@ -271,7 +270,7 @@ impl<V> ValuesSize<V> for RepeatedField<V::Inner>
}

impl<V> ValuesSize<V> for RepeatedField<V::Inner>
where V: raw::ConstSized + Wrapper
where V: raw::ConstSized
{
fn calculate_size(&self, builder: LengthBuilder) -> Option<LengthBuilder> {
let size = V::SIZE;
Expand Down
54 changes: 26 additions & 28 deletions protrust/src/extend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ use crate::Mergable;
use crate::collections::{RepeatedField, FieldSet, TryRead};
use crate::internal::Sealed;
use crate::io::{read::{self, Input}, write::{self, Output}, FieldNumber, WireType, Tag, LengthBuilder, CodedReader, CodedWriter};
use crate::raw::{Value, Packable, Packed};
use crate::raw::{ValueType, Value, Packable, Packed};
use hashbrown::{HashMap, hash_map::{self, DefaultHashBuilder}};
use trapper::Wrapper;

mod internal {
use alloc::boxed::Box;
Expand All @@ -21,9 +20,8 @@ mod internal {
use crate::{Mergable, merge};
use crate::collections::{RepeatedField, RepeatedValue};
use crate::io::{read, write, FieldNumber, WireType, Tag, LengthBuilder, CodedReader, CodedWriter};
use crate::raw::{Value, Packable, Packed};
use crate::raw::{ValueType, Value, Packable, Packed};
use super::ExtendableMessage;
use trapper::Wrapper;

pub trait ExtensionIdentifier: Sync {
fn field_number(&self) -> FieldNumber;
Expand Down Expand Up @@ -58,26 +56,26 @@ mod internal {
fn is_initialized(&self) -> bool;
}

pub struct ExtensionValue<V: Wrapper> {
pub struct ExtensionValue<V: ValueType> {
pub value: V::Inner,
pub num: FieldNumber,
}

impl<V: Wrapper> AsRef<V::Inner> for ExtensionValue<V> {
impl<V: ValueType> AsRef<V::Inner> for ExtensionValue<V> {
fn as_ref(&self) -> &V::Inner {
&self.value
}
}

impl<V: Wrapper> AsMut<V::Inner> for ExtensionValue<V> {
impl<V: ValueType> AsMut<V::Inner> for ExtensionValue<V> {
fn as_mut(&mut self) -> &mut V::Inner {
&mut self.value
}
}

impl<V> AnyExtension for ExtensionValue<V>
where
V: Value + Wrapper + 'static,
V: Value + 'static,
V::Inner: Clone + Mergable + PartialEq + Debug + Send + Sync
{
fn clone_into_box(&self) -> Box<dyn AnyExtension> {
Expand Down Expand Up @@ -116,40 +114,40 @@ mod internal {
output.write_field::<V>(self.num, &self.value)
}
fn is_initialized(&self) -> bool {
V::wrap_ref(&self.value).is_initialized()
V::is_initialized(&self.value)
}
}

impl<V> Debug for ExtensionValue<V>
where
V: Wrapper + 'static,
V: ValueType + 'static,
V::Inner: Debug
{
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
self.value.fmt(f)
}
}

pub struct RepeatedExtensionValue<V: Wrapper> {
pub struct RepeatedExtensionValue<V: ValueType> {
pub value: RepeatedField<V::Inner>,
pub num: FieldNumber,
}

impl<V: Wrapper> AsRef<RepeatedField<V::Inner>> for RepeatedExtensionValue<V> {
impl<V: ValueType> AsRef<RepeatedField<V::Inner>> for RepeatedExtensionValue<V> {
fn as_ref(&self) -> &RepeatedField<V::Inner> {
&self.value
}
}

impl<V: Wrapper> AsMut<RepeatedField<V::Inner>> for RepeatedExtensionValue<V> {
impl<V: ValueType> AsMut<RepeatedField<V::Inner>> for RepeatedExtensionValue<V> {
fn as_mut(&mut self) -> &mut RepeatedField<V::Inner> {
&mut self.value
}
}

impl<V> AnyExtension for RepeatedExtensionValue<V>
where
V: Value + Wrapper + 'static,
V: Value + 'static,
V::Inner: Clone + PartialEq + Debug + Send + Sync
{
fn clone_into_box(&self) -> Box<dyn AnyExtension> {
Expand Down Expand Up @@ -199,7 +197,7 @@ mod internal {

impl<V> AnyExtension for RepeatedExtensionValue<Packed<V>>
where
V: Value + Packable + Wrapper + 'static,
V: Value + Packable + 'static,
V::Inner: Clone + PartialEq + Debug + Send + Sync
{
fn clone_into_box(&self) -> Box<dyn AnyExtension> {
Expand Down Expand Up @@ -247,7 +245,7 @@ mod internal {
}
}

impl<V: Wrapper> Debug for RepeatedExtensionValue<V>
impl<V: ValueType> Debug for RepeatedExtensionValue<V>
where V::Inner: Debug
{
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
Expand All @@ -270,9 +268,9 @@ pub trait ExtendableMessage: Sized {
}

/// An extension identifier for accessing an extension value from an ExtensionSet
pub struct Extension<T, V, D = <V as Wrapper>::Inner>
pub struct Extension<T, V, D = <V as ValueType>::Inner>
where
V: Wrapper,
V: ValueType,
V::Inner: Borrow<D>,
D: ?Sized + ToOwned<Owned = V::Inner> + 'static {
t: PhantomData<fn(T) -> V>,
Expand All @@ -283,7 +281,7 @@ pub struct Extension<T, V, D = <V as Wrapper>::Inner>
impl<T, V, D> ExtensionIdentifier for Extension<T, V, D>
where
T: ExtendableMessage + 'static,
V: Value + Wrapper + 'static,
V: Value + 'static,
V::Inner: Mergable + Borrow<D> + PartialEq + Clone + Debug + Send + Sync,
D: ?Sized + ToOwned<Owned = V::Inner> + Sync + 'static
{
Expand All @@ -306,7 +304,7 @@ impl<T, V, D> ExtensionIdentifier for Extension<T, V, D>
impl<T, V, D> ExtensionType for Extension<T, V, D>
where
T: ExtendableMessage + 'static,
V: Value + Wrapper + 'static,
V: Value + 'static,
V::Inner: Mergable + Borrow<D> + PartialEq + Clone + Debug + Send + Sync,
D: ?Sized + ToOwned<Owned = V::Inner> + Sync + 'static
{
Expand All @@ -328,7 +326,7 @@ impl<T, V, D> ExtensionType for Extension<T, V, D>
#[doc(hidden)]
impl<T, V, D> Extension<T, V, D>
where
V: Wrapper,
V: ValueType,
V::Inner: Borrow<D>,
D: ?Sized + ToOwned<Owned = V::Inner> + 'static {
pub const fn with_static_default(num: FieldNumber, default: &'static D) -> Self {
Expand All @@ -350,7 +348,7 @@ impl<T, V, D> Extension<T, V, D>
#[doc(hidden)]
impl<T, V, D> Extension<T, V, D>
where
V: Wrapper,
V: ValueType,
V::Inner: Borrow<D>,
D: Sized + ToOwned<Owned = V::Inner> + 'static {
pub const fn with_owned_default(num: FieldNumber, default: V::Inner) -> Self {
Expand All @@ -363,15 +361,15 @@ impl<T, V, D> Extension<T, V, D>
}

/// An extension identifier for accessing a repeated extension value from an `ExtensionSet`
pub struct RepeatedExtension<T, V: Wrapper> {
pub struct RepeatedExtension<T, V: ValueType> {
t: PhantomData<fn(T) -> RepeatedField<V::Inner>>,
num: FieldNumber
}

impl<T, V> ExtensionIdentifier for RepeatedExtension<T, V>
where
T: ExtendableMessage + 'static,
V: Value + Wrapper + 'static,
V: Value + 'static,
V::Inner: Clone + PartialEq + Debug + Send + Sync
{
fn field_number(&self) -> FieldNumber {
Expand All @@ -397,7 +395,7 @@ impl<T, V> ExtensionIdentifier for RepeatedExtension<T, V>
impl<T, V> ExtensionIdentifier for RepeatedExtension<T, Packed<V>>
where
T: ExtendableMessage + 'static,
V: Value + Packable + Wrapper + 'static,
V: Value + Packable + 'static,
V::Inner: Clone + PartialEq + Debug + Send + Sync
{
fn field_number(&self) -> FieldNumber {
Expand All @@ -423,7 +421,7 @@ impl<T, V> ExtensionIdentifier for RepeatedExtension<T, Packed<V>>
impl<T, V> ExtensionType for RepeatedExtension<T, V>
where
T: ExtendableMessage + 'static,
V: Value + Wrapper + 'static,
V: Value + 'static,
V::Inner: Clone + PartialEq + Debug + Send + Sync
{
type Entry = internal::RepeatedExtensionValue<V>;
Expand All @@ -445,7 +443,7 @@ impl<T, V> ExtensionType for RepeatedExtension<T, V>
impl<T, V> ExtensionType for RepeatedExtension<T, Packed<V>>
where
T: ExtendableMessage + 'static,
V: Value + Packable + Wrapper + 'static,
V: Value + Packable + 'static,
V::Inner: Clone + PartialEq + Debug + Send + Sync
{
type Entry = internal::RepeatedExtensionValue<Packed<V>>;
Expand Down Expand Up @@ -588,7 +586,7 @@ impl<T: ExtendableMessage + 'static> ExtensionSet<T> {
/// Gets the value of the specified extension from the set or the default value for the extension if it exists
pub fn value_or_default<'a, 'e: 'a, V, D>(&'a self, extension: &'e Extension<T, V, D>) -> Option<&'a D>
where
V: Wrapper + Value + 'static,
V: Value + 'static,
V::Inner: Borrow<D> + Mergable + Clone + PartialEq + Debug + Send + Sync,
D: ?Sized + ToOwned<Owned = V::Inner> + Sync + 'static
{
Expand Down
9 changes: 4 additions & 5 deletions protrust/src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use core::num::NonZeroU32;
use core::slice;
use crate::collections::{RepeatedValue, FieldSet};
use crate::raw::Value;
use trapper::Wrapper;

#[cfg(feature = "std")]
use std::error::Error;
Expand Down Expand Up @@ -309,7 +308,7 @@ impl Length {
}

/// Returns the length of the value in the specified form
pub fn of_value<V: Value + Wrapper>(value: &V::Inner) -> Option<Length> {
pub fn of_value<V: Value>(value: &V::Inner) -> Option<Length> {
LengthBuilder::new().add_value::<V>(value).map(LengthBuilder::build)
}

Expand Down Expand Up @@ -363,13 +362,13 @@ impl LengthBuilder {
/// Adds a value's length to this instance
#[inline]
#[must_use = "this returns the builder to chain and does not mutate it in place"]
pub fn add_value<V: Value + Wrapper>(self, value: &V::Inner) -> Option<Self> {
V::wrap_ref(value).calculate_size(self)
pub fn add_value<V: Value>(self, value: &V::Inner) -> Option<Self> {
V::calculate_size(value, self)
}
/// Adds a field's length to this instance using the specified field number
#[inline]
#[must_use = "this returns the builder to chain and does not mutate it in place"]
pub fn add_field<V: Value + Wrapper>(self, num: FieldNumber, value: &V::Inner) -> Option<Self> {
pub fn add_field<V: Value>(self, num: FieldNumber, value: &V::Inner) -> Option<Self> {
let temp =
self.add_tag(Tag::new(num, V::WIRE_TYPE))?
.add_value::<V>(value)?;
Expand Down
9 changes: 4 additions & 5 deletions protrust/src/io/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use crate::collections::{RepeatedValue, FieldSet, TryRead};
use crate::extend::ExtensionRegistry;
use crate::io::{Tag, WireType, ByteString, stream::{self, Read}};
use crate::raw::{self, Value};
use trapper::Wrapper;

#[cfg(feature = "std")]
use std::error;
Expand Down Expand Up @@ -1584,14 +1583,14 @@ impl<T: Input> CodedReader<T> {
/// Reads a new instance of the value from the reader.
/// This is the inverse of `Value::read_new`.
#[inline]
pub fn read_value<V: Value + Wrapper>(&mut self) -> Result<V::Inner> {
V::read_new(self).map(V::unwrap)
pub fn read_value<V: Value>(&mut self) -> Result<V::Inner> {
V::read_new(self)
}
/// Merges the reader's value with the value from the reader.
/// This is the inverse of `Value::merge_from`.
#[inline]
pub fn merge_value<V: Value + Wrapper>(&mut self, value: &mut V::Inner) -> Result<()> {
V::wrap_mut(value).merge_from(self)
pub fn merge_value<V: Value>(&mut self, value: &mut V::Inner) -> Result<()> {
V::merge_from(value, self)
}
/// Adds field entries from the reader to the specified value.
/// This is the inverse of `RepeatedValue::add_entries_from`.
Expand Down
7 changes: 3 additions & 4 deletions protrust/src/io/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use crate::collections::{RepeatedValue, FieldSet};
use crate::io::{FieldNumber, WireType, Tag, Length, stream::{self, Write}};
use crate::raw::Value;
use super::{raw_varint32_size, raw_varint64_size};
use trapper::Wrapper;

#[cfg(feature = "std")]
use std::error;
Expand Down Expand Up @@ -691,12 +690,12 @@ impl<T: Output> CodedWriter<T> {

/// Writes the value to the output. This uses an alias to `Value::write_to`.
#[inline]
pub fn write_value<V: Value + Wrapper>(&mut self, value: &V::Inner) -> Result {
V::wrap_ref(value).write_to(self)
pub fn write_value<V: Value>(&mut self, value: &V::Inner) -> Result {
V::write_to(value, self)
}
/// Writes the value to the output using the field number and the wire type of the value.
#[inline]
pub fn write_field<V: Value + Wrapper>(&mut self, num: FieldNumber, value: &V::Inner) -> Result {
pub fn write_field<V: Value>(&mut self, num: FieldNumber, value: &V::Inner) -> Result {
self.write_tag(Tag::new(num, V::WIRE_TYPE))?;
self.write_value::<V>(value)?;
if V::WIRE_TYPE != WireType::StartGroup {
Expand Down
1 change: 0 additions & 1 deletion protrust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! A fast, feature complete, protobuf implementation.
#![feature(const_fn)]
#![feature(read_initializer)]
#![feature(specialization)]
#![feature(box_into_raw_non_null)]
#![feature(new_uninit)]
Expand Down
Loading

0 comments on commit 42caf59

Please sign in to comment.