Skip to content
Closed
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
Merge branch 'master' into dp-flag-types-as-compact
  • Loading branch information
dvdplm committed Jan 4, 2021
commit f098101ef075503f4daa6e04d2adaa20626bcc5f
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,9 @@ identifiers.
All concrete `TypeInfo` structures have two forms:

- One meta form (`MetaType`) that acts as a bridge to other forms
- A frozen form suitable for serialization.
- A portable form suitable for serialization.

The `IntoFrozen` trait must also be implemented in order prepare a type
The `IntoPortable` trait must also be implemented in order prepare a type
definition for serialization using an instance of the type registry.

After transformation all type definitions are stored in the type registry.
Expand Down
16 changes: 8 additions & 8 deletions src/form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
//! It uses `MetaType` for communicating type identifiers and thus acts as
//! a bridge from runtime to compile time type information.
//!
//! The `FrozenForm` is a space-efficient representation
//! The `PortableForm` is a space-efficient representation
//! that no longer has any connections to the interning registry and thus
//! can no longer be used to retrieve information from the
//! original registry. Its sole purpose is for space-efficient serialization.
//!
//! Other forms, such as a frozen form that is still bound to the registry
//! Other forms, such as a portable form that is still bound to the registry
//! (also via lifetime tracking) are possible but current not needed.

use crate::prelude::{
Expand All @@ -47,7 +47,7 @@ use serde::Serialize;
/// Trait to control the internal structures of type definitions.
///
/// This allows for type-level separation between free forms that can be
/// instantiated out of the flux and frozen forms that require some sort of
/// instantiated out of the flux and portable forms that require some sort of
/// interning data structures.
pub trait Form {
/// The type representing the type.
Expand All @@ -67,8 +67,8 @@ impl FormString for String {}

/// A meta meta-type.
///
/// Allows to be converted into other forms such as frozen form
/// through the registry and `IntoFrozen`.
/// Allows to be converted into other forms such as portable form
/// through the registry and `IntoPortable`.
#[cfg_attr(feature = "serde", derive(Serialize))]
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Debug)]
pub enum MetaForm {}
Expand All @@ -78,7 +78,7 @@ impl Form for MetaForm {
type String = &'static str;
}

/// Frozen form that has its lifetime untracked in association to its interner.
/// Portable form that has its lifetime untracked in association to its interner.
///
/// # Note
///
Expand All @@ -89,9 +89,9 @@ impl Form for MetaForm {
/// `type String` is owned in order to enable decoding
#[cfg_attr(feature = "serde", derive(Serialize))]
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Debug)]
pub struct FrozenForm<S = &'static str>(PhantomData<S>);
pub struct PortableForm<S = &'static str>(PhantomData<S>);

impl<S> Form for FrozenForm<S>
impl<S> Form for PortableForm<S>
where
S: FormString,
{
Expand Down
14 changes: 7 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
//!
//! This library provides structures to easily retrieve compile-time type
//! information at runtime and also to serialize this information in a
//! space-efficient form, aka `FrozenForm`.
//! space-efficient form, aka `PortableForm`.
//!
//! # Registry
//!
Expand All @@ -38,12 +38,12 @@
//!
//! # Forms
//!
//! There is an expanded form, called [`MetaForm`](`crate::form::MetaForm`) that
//! acts as a bridge between compile-time type information and runtime, in order
//! to easily retrieve all information needed to uniquely identify types.
//! To bridge between compile-time type information and runtime the
//! [`MetaForm`](`crate::form::MetaForm`) is used to easily retrieve all
//! information needed to uniquely identify types.
//!
//! The `MetaForm` and its associated `Registry` can be transformed into the
//! space-efficient form by the [`IntoFrozen`](`crate::IntoFrozen`) trait; it is
//! space-efficient form by the [`IntoPortable`](`crate::IntoPortable`) trait; it is
//! used internally by the [`Registry`](`crate::Registry`) in order to convert
//! the expanded types into their space-efficient form.
//!
Expand All @@ -59,7 +59,7 @@
//! initially with your own data structures; make them generic over the
//! [`Form`](`crate::form::Form`) trait just as has been done in this crate with
//! [`TypeInfo`](`crate::TypeInfo`) in order to get a simple implementation of
//! [`IntoFrozen`](`crate::IntoFrozen`). Use a single instance of the
//! [`IntoPortable`](`crate::IntoPortable`). Use a single instance of the
//! [`Registry`](`crate::Registry`) for compaction and provide this registry
//! instance upon serialization.
//!
Expand Down Expand Up @@ -119,7 +119,7 @@ mod tests;
pub use self::{
meta_type::MetaType,
registry::{
IntoFrozen,
IntoPortable,
Registry,
RegistryReadOnly,
},
Expand Down
44 changes: 23 additions & 21 deletions src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use crate::{
form::{
Form,
FormString,
FrozenForm,
PortableForm,
},
interner::{
Interner,
Expand All @@ -56,19 +56,19 @@ use serde::{
Serialize,
};

/// Freezes the type definition using a registry.
pub trait IntoFrozen {
/// The frozen version of `Self`.
/// Convert the type definition into the portable form using a registry.
pub trait IntoPortable {
/// The portable version of `Self`.
type Output;

/// "Freezes" `self` by using the registry for caching.
fn into_frozen(self, registry: &mut Registry) -> Self::Output;
/// Convert `self` to the portable form by using the registry for caching.
fn into_portable(self, registry: &mut Registry) -> Self::Output;
}

impl IntoFrozen for &'static str {
type Output = <FrozenForm as Form>::String;
impl IntoPortable for &'static str {
type Output = <PortableForm as Form>::String;

fn into_frozen(self, _registry: &mut Registry) -> Self::Output {
fn into_portable(self, _registry: &mut Registry) -> Self::Output {
self
}
}
Expand Down Expand Up @@ -99,14 +99,14 @@ pub struct Registry {
///
/// The contents herein is used for serlialization.
#[cfg_attr(feature = "serde", serde(serialize_with = "serialize_registry_types"))]
types: BTreeMap<UntrackedSymbol<core::any::TypeId>, Type<FrozenForm>>,
types: BTreeMap<UntrackedSymbol<core::any::TypeId>, Type<PortableForm>>,
}

/// Serializes the types of the registry by removing their unique IDs and
/// serializes them in order of their removed unique ID.
#[cfg(feature = "serde")]
fn serialize_registry_types<S>(
types: &BTreeMap<UntrackedSymbol<core::any::TypeId>, Type<FrozenForm>>,
types: &BTreeMap<UntrackedSymbol<core::any::TypeId>, Type<PortableForm>>,
serializer: S,
) -> Result<S::Ok, S::Error>
where
Expand All @@ -124,7 +124,7 @@ impl Default for Registry {

impl Encode for Registry {
fn size_hint(&self) -> usize {
mem::size_of::<u32>() + mem::size_of::<Type<FrozenForm>>() * self.types.len()
mem::size_of::<u32>() + mem::size_of::<Type<PortableForm>>() * self.types.len()
}

fn encode_to<W: scale::Output>(&self, dest: &mut W) {
Expand Down Expand Up @@ -175,8 +175,8 @@ impl Registry {
pub fn register_type(&mut self, ty: &MetaType) -> UntrackedSymbol<TypeId> {
let (inserted, symbol) = self.intern_type_id(ty.type_id());
if inserted {
let frozen_id = ty.type_info().into_frozen(self);
self.types.insert(symbol, frozen_id);
let portable_id = ty.type_info().into_portable(self);
self.types.insert(symbol, portable_id);
}
symbol
}
Expand All @@ -191,15 +191,15 @@ impl Registry {
.collect::<Vec<_>>()
}

/// Converts an iterator into a Vec of the equivalent frozen
/// Converts an iterator into a Vec of the equivalent portable
/// representations.
pub fn map_into_frozen<I, T>(&mut self, iter: I) -> Vec<T::Output>
pub fn map_into_portable<I, T>(&mut self, iter: I) -> Vec<T::Output>
where
I: IntoIterator<Item = T>,
T: IntoFrozen,
T: IntoPortable,
{
iter.into_iter()
.map(|i| i.into_frozen(self))
.map(|i| i.into_portable(self))
.collect::<Vec<_>>()
}
}
Expand All @@ -215,7 +215,7 @@ pub struct RegistryReadOnly<S = &'static str>
where
S: FormString,
{
types: Vec<Type<FrozenForm<S>>>,
types: Vec<Type<PortableForm<S>>>,
}

impl From<Registry> for RegistryReadOnly {
Expand All @@ -231,12 +231,14 @@ where
S: FormString,
{
/// Returns the type definition for the given identifier, `None` if no type found for that ID.
pub fn resolve(&self, id: NonZeroU32) -> Option<&Type<FrozenForm<S>>> {
pub fn resolve(&self, id: NonZeroU32) -> Option<&Type<PortableForm<S>>> {
self.types.get((id.get() - 1) as usize)
}

/// Returns an iterator for all types paired with their associated NonZeroU32 identifier.
pub fn enumerate(&self) -> impl Iterator<Item = (NonZeroU32, &Type<FrozenForm<S>>)> {
pub fn enumerate(
&self,
) -> impl Iterator<Item = (NonZeroU32, &Type<PortableForm<S>>)> {
self.types.iter().enumerate().map(|(i, ty)| {
let id = NonZeroU32::new(i as u32 + 1).expect("i + 1 > 0; qed");
(id, ty)
Expand Down
11 changes: 5 additions & 6 deletions src/ty/composite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ use crate::prelude::vec::Vec;
use crate::{
form::{
Form,
FrozenForm,
MetaForm,
PortableForm,
},
Field,
IntoFrozen,
IntoPortable,
Registry,
};
use derive_more::From;
Expand Down Expand Up @@ -82,12 +81,12 @@ pub struct TypeDefComposite<T: Form = MetaForm> {
fields: Vec<Field<T>>,
}

impl IntoFrozen for TypeDefComposite {
type Output = TypeDefComposite<FrozenForm>;
impl IntoPortable for TypeDefComposite {
type Output = TypeDefComposite<PortableForm>;

fn into_frozen(self, registry: &mut Registry) -> Self::Output {
fn into_portable(self, registry: &mut Registry) -> Self::Output {
TypeDefComposite {
fields: registry.map_into_frozen(self.fields),
fields: registry.map_into_portable(self.fields),
}
}
}
Expand Down
13 changes: 6 additions & 7 deletions src/ty/fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@
use crate::{
form::{
Form,
FrozenForm,
MetaForm,
PortableForm,
},
IntoFrozen,
IntoPortable,
MetaType,
Registry,
TypeInfo,
Expand Down Expand Up @@ -100,14 +99,14 @@ const fn is_false(v: &bool) -> bool {
!(*v)
}

impl IntoFrozen for Field {
type Output = Field<FrozenForm>;
impl IntoPortable for Field {
type Output = Field<PortableForm>;

fn into_frozen(self, registry: &mut Registry) -> Self::Output {
fn into_portable(self, registry: &mut Registry) -> Self::Output {
Field {
name: self.name.map(|name| name.into_frozen(registry)),
name: self.name.map(|name| name.into_portable(registry)),
ty: registry.register_type(&self.ty),
type_name: self.type_name.into_frozen(registry),
type_name: self.type_name.into_portable(registry),
compact: self.compact,
}
}
Expand Down
47 changes: 23 additions & 24 deletions src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ use crate::{
build::TypeBuilder,
form::{
Form,
FrozenForm,
MetaForm,
PortableForm,
},
IntoFrozen,
IntoPortable,
MetaType,
Registry,
TypeInfo,
Expand Down Expand Up @@ -83,14 +82,14 @@ pub struct Type<T: Form = MetaForm> {
type_def: TypeDef<T>,
}

impl IntoFrozen for Type {
type Output = Type<FrozenForm>;
impl IntoPortable for Type {
type Output = Type<PortableForm>;

fn into_frozen(self, registry: &mut Registry) -> Self::Output {
fn into_portable(self, registry: &mut Registry) -> Self::Output {
Type {
path: self.path.into_frozen(registry),
path: self.path.into_portable(registry),
type_params: registry.register_types(self.type_params),
type_def: self.type_def.into_frozen(registry),
type_def: self.type_def.into_portable(registry),
}
}
}
Expand Down Expand Up @@ -184,16 +183,16 @@ pub enum TypeDef<T: Form = MetaForm> {
Primitive(TypeDefPrimitive),
}

impl IntoFrozen for TypeDef {
type Output = TypeDef<FrozenForm>;
impl IntoPortable for TypeDef {
type Output = TypeDef<PortableForm>;

fn into_frozen(self, registry: &mut Registry) -> Self::Output {
fn into_portable(self, registry: &mut Registry) -> Self::Output {
match self {
TypeDef::Composite(composite) => composite.into_frozen(registry).into(),
TypeDef::Variant(variant) => variant.into_frozen(registry).into(),
TypeDef::Sequence(sequence) => sequence.into_frozen(registry).into(),
TypeDef::Array(array) => array.into_frozen(registry).into(),
TypeDef::Tuple(tuple) => tuple.into_frozen(registry).into(),
TypeDef::Composite(composite) => composite.into_portable(registry).into(),
TypeDef::Variant(variant) => variant.into_portable(registry).into(),
TypeDef::Sequence(sequence) => sequence.into_portable(registry).into(),
TypeDef::Array(array) => array.into_portable(registry).into(),
TypeDef::Tuple(tuple) => tuple.into_portable(registry).into(),
TypeDef::Primitive(primitive) => primitive.into(),
}
}
Expand Down Expand Up @@ -254,10 +253,10 @@ pub struct TypeDefArray<T: Form = MetaForm> {
type_param: T::Type,
}

impl IntoFrozen for TypeDefArray {
type Output = TypeDefArray<FrozenForm>;
impl IntoPortable for TypeDefArray {
type Output = TypeDefArray<PortableForm>;

fn into_frozen(self, registry: &mut Registry) -> Self::Output {
fn into_portable(self, registry: &mut Registry) -> Self::Output {
TypeDefArray {
len: self.len,
type_param: registry.register_type(&self.type_param),
Expand Down Expand Up @@ -304,10 +303,10 @@ pub struct TypeDefTuple<T: Form = MetaForm> {
fields: Vec<T::Type>,
}

impl IntoFrozen for TypeDefTuple {
type Output = TypeDefTuple<FrozenForm>;
impl IntoPortable for TypeDefTuple {
type Output = TypeDefTuple<PortableForm>;

fn into_frozen(self, registry: &mut Registry) -> Self::Output {
fn into_portable(self, registry: &mut Registry) -> Self::Output {
TypeDefTuple {
fields: registry.register_types(self.fields),
}
Expand Down Expand Up @@ -357,10 +356,10 @@ pub struct TypeDefSequence<T: Form = MetaForm> {
type_param: T::Type,
}

impl IntoFrozen for TypeDefSequence {
type Output = TypeDefSequence<FrozenForm>;
impl IntoPortable for TypeDefSequence {
type Output = TypeDefSequence<PortableForm>;

fn into_frozen(self, registry: &mut Registry) -> Self::Output {
fn into_portable(self, registry: &mut Registry) -> Self::Output {
TypeDefSequence {
type_param: registry.register_type(&self.type_param),
}
Expand Down
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.