Skip to content

Commit

Permalink
Fix musli_wire::Typed
Browse files Browse the repository at this point in the history
  • Loading branch information
udoprog committed Apr 15, 2024
1 parent 6de87b0 commit a00d5b6
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 77 deletions.
2 changes: 1 addition & 1 deletion crates/musli-descriptive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,6 @@ pub use self::error::Error;
#[cfg(feature = "test")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "test")))]
#[doc(hidden)]
pub use self::test::{transcode, Typed};
pub use self::test::transcode;

musli_utils::simdutf8!();
38 changes: 1 addition & 37 deletions crates/musli-descriptive/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,8 @@

use core::fmt::Debug;

use musli::de::PackDecoder;
use musli::mode::DefaultMode;
use musli::{Decode, Decoder, Encode};

use crate::tag::Tag;

/// A typed field, which is prefixed with a type tag.
///
/// This is used in combination with the storage deserializer to "inspect" type
/// tags.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Typed<T> {
tag: Tag,
value: T,
}

impl<T> Typed<T> {
/// Construct a new typed field.
pub const fn new(tag: Tag, value: T) -> Self {
Self { tag, value }
}
}

impl<'de, M, T> Decode<'de, M> for Typed<T>
where
T: Decode<'de, M>,
{
fn decode<D>(_: &D::Cx, decoder: D) -> Result<Self, D::Error>
where
D: Decoder<'de, Mode = M>,
{
decoder.decode_pack(|pack| {
let tag = pack.decode_next()?.decode()?;
let value = pack.decode_next()?.decode()?;
Ok(Self { tag, value })
})
}
}
use musli::{Decode, Encode};

musli_utils::test_fns!("descriptive", #[musli_value]);

Expand Down
15 changes: 4 additions & 11 deletions crates/musli-wire/src/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
use core::fmt;
use core::mem;

use musli::{Decode, Decoder};
#[cfg(feature = "test")]
use musli::{Decode, Encode};

/// Data masked into the data type.
pub(crate) const DATA_MASK: u8 = 0b00_111111;
Expand Down Expand Up @@ -40,7 +41,9 @@ pub enum Kind {
/// question. It is primarily used to smuggle extra data for the kind in
/// question.
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "test", derive(Encode, Decode))]
#[repr(transparent)]
#[cfg_attr(feature = "test", musli(transparent))]
pub struct Tag {
/// The internal representation of the tag.
repr: u8,
Expand Down Expand Up @@ -141,13 +144,3 @@ impl fmt::Debug for Tag {
.finish()
}
}

impl<'de, M> Decode<'de, M> for Tag {
#[inline]
fn decode<D>(_: &D::Cx, decoder: D) -> Result<Self, D::Error>
where
D: Decoder<'de, Mode = M>,
{
Ok(Self::from_byte(decoder.decode()?))
}
}
31 changes: 8 additions & 23 deletions crates/musli-wire/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,30 @@

use core::fmt::Debug;

use musli::de::PackDecoder;
use musli::mode::DefaultMode;
use musli::{Decode, Decoder, Encode};
use musli::{Decode, Encode};

use crate::tag::Tag;

/// A typed field, which is prefixed with a type tag.
///
/// This is used in combination with the storage deserializer to "inspect" type
/// tags.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Typed<T> {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Encode, Decode)]
#[musli(packed)]
pub struct Typed<const N: usize> {
tag: Tag,
value: T,
#[musli(bytes)]
value: [u8; N],
}

impl<T> Typed<T> {
impl<const N: usize> Typed<N> {
/// Construct a new typed field.
pub const fn new(tag: Tag, value: T) -> Self {
pub const fn new(tag: Tag, value: [u8; N]) -> Self {
Self { tag, value }
}
}

impl<'de, M, T> Decode<'de, M> for Typed<T>
where
T: Decode<'de, M>,
{
fn decode<D>(_: &D::Cx, decoder: D) -> Result<Self, D::Error>
where
D: Decoder<'de, Mode = M>,
{
decoder.decode_pack(|pack| {
let tag = pack.decode_next()?.decode()?;
let value = pack.decode_next()?.decode()?;
Ok(Self { tag, value })
})
}
}

musli_utils::test_fns!("wire");

/// Encode a type as one and decode as another.
Expand Down
2 changes: 1 addition & 1 deletion tests/tests/numbers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ struct Unpacked {
b: (Tag, Tag),
c: (Tag, Tag),
d: (Tag, Tag),
e: (Tag, Typed<[u8; 5]>),
e: (Tag, Typed<5>),
}

#[test]
Expand Down
8 changes: 4 additions & 4 deletions tests/tests/struct_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ fn named_struct_unpack() {
#[musli(packed)]
pub struct Unpacked {
field_count: Tag,
field1_name: Typed<[u8; 6]>,
field1_value: Typed<[u8; 3]>,
field2_name: Typed<[u8; 6]>,
field1_name: Typed<6>,
field1_value: Typed<3>,
field2_name: Typed<6>,
field2_value: Tag,
}

Expand Down Expand Up @@ -122,7 +122,7 @@ fn indexed_struct_unpack() {
pub struct Unpacked {
field_count: Tag,
field1_index: Tag,
field1_value: Typed<[u8; 3]>,
field1_value: Typed<3>,
field2_index: Tag,
field2_value: Tag,
}
Expand Down

0 comments on commit a00d5b6

Please sign in to comment.