Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Remove OwnedForm, use a String for CompactForm
  • Loading branch information
ascjones committed Aug 18, 2020
commit b217127267701ff5c3f1d6bfeb55b92f47295bdc
11 changes: 0 additions & 11 deletions src/form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,5 @@ pub enum CompactForm {}

impl Form for CompactForm {
type TypeId = UntrackedSymbol<TypeId>;
type String = &'static str;
}

/// Owned form for decoding
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Serialize, Debug)]
pub enum OwnedForm {}

impl Form for OwnedForm {
/// For correctness should be [`NonZeroU32`](`core::num::NonZeroU32`), but the current
/// (`1.3.x`) release of `parity-scale-codec` does not include the `NoneZero*` Decode impls.
type TypeId = u32;
type String = String;
}
16 changes: 14 additions & 2 deletions src/interner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,37 @@
//! elements and is later used for compact serialization within the registry.

use crate::tm_std::*;
use serde::Serialize;
use serde::{Serialize, Deserialize};

/// A symbol that is not lifetime tracked.
///
/// This can be used by self-referential types but
/// can no longer be used to resolve instances.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(transparent)]
pub struct UntrackedSymbol<T> {
id: NonZeroU32,
#[serde(skip)]
marker: PhantomData<fn() -> T>,
}

impl<T> scale::Encode for UntrackedSymbol<T> {
fn encode_to<W: scale::Output>(&self, dest: &mut W) {
self.id.get().encode_to(dest)
}
}

impl<T> scale::Decode for UntrackedSymbol<T> {
fn decode<I: scale::Input>(value: &mut I) -> Result<Self, scale::Error> {
let id = <u32 as scale::Decode>::decode(value)?;
if id < 1 {
return Err("UntrackedSymbol::id should be a non-zero unsigned integer".into());
}
let id = NonZeroU32::new(id).expect("ID is non zero");
Ok(UntrackedSymbol { id, marker: Default::default() })
}
}

/// A symbol from an interner.
///
/// Can be used to resolve to the associated instance.
Expand Down
14 changes: 11 additions & 3 deletions src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

use crate::tm_std::*;
use crate::{
form::{CompactForm, OwnedForm},
form::{CompactForm, Form},
interner::{Interner, UntrackedSymbol},
meta_type::MetaType,
Type,
Expand All @@ -43,6 +43,14 @@ pub trait IntoCompact {
fn into_compact(self, registry: &mut Registry) -> Self::Output;
}

impl IntoCompact for &'static str {
type Output = <CompactForm as Form>::String;

fn into_compact(self, _registry: &mut Registry) -> Self::Output {
self.to_owned()
}
}

/// The registry for compaction of type identifiers and definitions.
///
/// The registry consists of a cache for already compactified type identifiers and definitions.
Expand Down Expand Up @@ -170,12 +178,12 @@ impl Registry {
/// A read-only registry, to be used for decoding/deserializing
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Decode)]
pub struct RegistryReadOnly {
types: Vec<Type<OwnedForm>>,
types: Vec<Type<CompactForm>>,
}

impl RegistryReadOnly {
/// Returns the type definition for the given identifier, `None` if no type found for that ID.
pub fn resolve(&self, id: NonZeroU32) -> Option<&Type<OwnedForm>> {
pub fn resolve(&self, id: NonZeroU32) -> Option<&Type<CompactForm>> {
self.types.get((id.get() - 1) as usize);
None
}
Expand Down
2 changes: 1 addition & 1 deletion src/ty/fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl IntoCompact for Field {

fn into_compact(self, registry: &mut Registry) -> Self::Output {
Field {
name: self.name,
name: self.name.map(|name| name.into_compact(registry)),
ty: registry.register_type(&self.ty),
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/ty/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ where
impl IntoCompact for Path {
type Output = Path<CompactForm>;

fn into_compact(self, _registry: &mut Registry) -> Self::Output {
fn into_compact(self, registry: &mut Registry) -> Self::Output {
Path {
segments: self.segments,
segments: registry.map_into_compact(self.segments),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/ty/variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ impl IntoCompact for Variant {

fn into_compact(self, registry: &mut Registry) -> Self::Output {
Variant {
name: self.name,
name: self.name.into_compact(registry),
fields: registry.map_into_compact(self.fields),
discriminant: self.discriminant,
}
Expand Down