-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for declarative CHOICE types #11
- Loading branch information
1 parent
474433c
commit 3f5fe3a
Showing
9 changed files
with
357 additions
and
28 deletions.
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use crate::syn::{ReadableType, Reader, WritableType, Writer}; | ||
use core::marker::PhantomData; | ||
|
||
pub struct Choice<C: Constraint>(PhantomData<C>); | ||
|
||
impl<C: Constraint> Default for Choice<C> { | ||
fn default() -> Self { | ||
Self(Default::default()) | ||
} | ||
} | ||
|
||
pub trait Constraint: Sized { | ||
const NAME: &'static str; | ||
const VARIANT_COUNT: usize; | ||
const STD_VARIANT_COUNT: usize; | ||
const EXTENSIBLE: bool = false; | ||
|
||
fn to_choice_index(&self) -> usize; | ||
|
||
fn write_content<W: Writer>(&self, writer: &mut W) -> Result<(), W::Error>; | ||
|
||
fn read_content<R: Reader>(index: usize, reader: &mut R) -> Result<Option<Self>, R::Error>; | ||
} | ||
|
||
impl<C: Constraint> WritableType for Choice<C> { | ||
type Type = C; | ||
|
||
#[inline] | ||
fn write_value<W: Writer>( | ||
writer: &mut W, | ||
value: &Self::Type, | ||
) -> Result<(), <W as Writer>::Error> { | ||
writer.write_choice(value) | ||
} | ||
} | ||
|
||
impl<C: Constraint> ReadableType for Choice<C> { | ||
type Type = C; | ||
|
||
#[inline] | ||
fn read_value<R: Reader>(reader: &mut R) -> Result<Self::Type, <R as Reader>::Error> { | ||
reader.read_choice::<Self::Type>() | ||
} | ||
} |
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
Oops, something went wrong.