Skip to content

Commit

Permalink
Merge pull request #28 from strict-types/phantom
Browse files Browse the repository at this point in the history
Support phantom data
  • Loading branch information
dr-orlovsky authored Dec 30, 2023
2 parents fb236fd + 2ac719c commit 28e29bf
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "strict_encoding"
version = "2.6.1"
version = "2.6.2"
description = "Strict encoding: deterministic & confined binary serialization for strict types"
keywords = ["strict-types", "gadt", "serialization", "serde", "protobuf"]
categories = ["encoding", "parsing"]
Expand Down
9 changes: 9 additions & 0 deletions rust/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
// limitations under the License.

use std::io::{BufRead, Seek};
use std::marker::PhantomData;
use std::{fs, io};

use amplify::confinement::{Collection, Confined};
Expand Down Expand Up @@ -335,6 +336,14 @@ impl<T: StrictEncode> StrictEncode for &T {
}
}

impl<T> StrictEncode for PhantomData<T> {
fn strict_encode<W: TypedWrite>(&self, writer: W) -> io::Result<W> { Ok(writer) }
}

impl<T> StrictDecode for PhantomData<T> {
fn strict_decode(_reader: &mut impl TypedRead) -> Result<Self, DecodeError> { Ok(default!()) }
}

pub trait StrictSerialize: StrictEncode {
fn strict_serialized_len(&self) -> io::Result<usize> {
let counter = StrictWriter::counter();
Expand Down
5 changes: 5 additions & 0 deletions rust/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use std::any;
use std::collections::BTreeSet;
use std::fmt::{Debug, Display};
use std::marker::PhantomData;

use crate::{FieldName, LibName, TypeName};

Expand Down Expand Up @@ -70,6 +71,10 @@ impl<T: StrictType> StrictType for &T {
const STRICT_LIB_NAME: &'static str = T::STRICT_LIB_NAME;
}

impl<T> StrictType for PhantomData<T> {
const STRICT_LIB_NAME: &'static str = "";
}

pub trait StrictProduct: StrictType + StrictDumb {}

pub trait StrictTuple: StrictProduct {
Expand Down

0 comments on commit 28e29bf

Please sign in to comment.