-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add BitVec support #98
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
dbee2da
Add BitVec support
ascjones 61df8b9
bit-vec feature
ascjones 31b361c
test features
ascjones 2f035e9
Allow decoding of TypeDefBitSequence without feature enabled
ascjones ddd9a75
Encapsulate impls in mod and add back From impl
ascjones 1c34c60
Fmt
ascjones File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -118,6 +118,13 @@ impl_from_type_def_for_type!( | |
TypeDefPhantom, | ||
); | ||
|
||
#[cfg(feature = "bit-vec")] | ||
impl From<crate::TypeDefBitVec> for Type { | ||
fn from(item: crate::TypeDefBitVec) -> Self { | ||
Self::new(Path::voldemort(), Vec::new(), item, Vec::new()) | ||
} | ||
} | ||
|
||
impl Type { | ||
/// Create a [`TypeBuilder`](`crate::build::TypeBuilder`) the public API for constructing a [`Type`] | ||
pub fn builder() -> TypeBuilder { | ||
|
@@ -197,6 +204,9 @@ pub enum TypeDef<T: Form = MetaForm> { | |
Compact(TypeDefCompact<T>), | ||
/// A PhantomData type. | ||
Phantom(TypeDefPhantom<T>), | ||
#[cfg(feature = "bit-vec")] | ||
/// A BitVec type. | ||
BitVec(TypeDefBitVec<T>), | ||
} | ||
|
||
impl IntoPortable for TypeDef { | ||
|
@@ -212,6 +222,8 @@ impl IntoPortable for TypeDef { | |
TypeDef::Primitive(primitive) => primitive.into(), | ||
TypeDef::Compact(compact) => compact.into_portable(registry).into(), | ||
TypeDef::Phantom(phantom) => phantom.into_portable(registry).into(), | ||
#[cfg(feature = "bit-vec")] | ||
TypeDef::BitVec(bitvec) => bitvec.into_portable(registry).into(), | ||
} | ||
} | ||
} | ||
|
@@ -485,3 +497,58 @@ where | |
&self.type_param | ||
} | ||
} | ||
|
||
/// Type describing a [`bitvec::vec::BitVec`]. | ||
#[cfg(feature = "bit-vec")] | ||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] | ||
#[cfg_attr(any(feature = "std", feature = "decode"), derive(scale::Decode))] | ||
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Encode, Debug)] | ||
pub struct TypeDefBitVec<T: Form = MetaForm> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd rename this to |
||
/// The type implementing [`::bitvec::store::BitStore`]. | ||
bit_store_type: T::Type, | ||
/// The type implementing [`::bitvec::order::BitOrder`]. | ||
bit_order_type: T::Type, | ||
} | ||
|
||
#[cfg(feature = "bit-vec")] | ||
impl IntoPortable for crate::TypeDefBitVec { | ||
type Output = crate::TypeDefBitVec<PortableForm>; | ||
|
||
fn into_portable(self, registry: &mut Registry) -> Self::Output { | ||
TypeDefBitVec { | ||
bit_store_type: registry.register_type(&self.bit_store_type), | ||
bit_order_type: registry.register_type(&self.bit_order_type), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(feature = "bit-vec")] | ||
impl TypeDefBitVec { | ||
/// Creates a new phantom type definition. | ||
ascjones marked this conversation as resolved.
Show resolved
Hide resolved
|
||
pub fn new<O, T>() -> Self | ||
where | ||
O: bitvec::order::BitOrder + TypeInfo + 'static, | ||
T: bitvec::store::BitStore + TypeInfo + 'static, | ||
{ | ||
Self { | ||
bit_order_type: MetaType::new::<O>(), | ||
bit_store_type: MetaType::new::<T>(), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(feature = "bit-vec")] | ||
impl<T> TypeDefBitVec<T> | ||
where | ||
T: Form, | ||
{ | ||
/// Returns the type of the bit ordering of the [`::bitvec::vec::BitVec`]. | ||
pub fn bit_order_type(&self) -> &T::Type { | ||
&self.bit_order_type | ||
} | ||
|
||
/// Returns underlying type used to store the [`::bitvec::vec::BitVec`]. | ||
pub fn bit_store_type(&self) -> &T::Type { | ||
&self.bit_store_type | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think dtolnay mentioned that for compatibility with respect to different crate feature sets it is better to replace the inner type of
BitVec
with a placeholder type (dummy) ofnot(feature = "bit-vec")
instead of entirely removing this variant.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is much better, will avoid issues with incompatible decoding if we add variants after this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've removed the feature flag from this variant now so it will be binary compatible, and also so that decoding/deserializing is supported even without the feature enabled.