Skip to content

Commit

Permalink
feat: impl CandidType for PhantomData (#591)
Browse files Browse the repository at this point in the history
This functionality is needed in
dfinity/cdk-rs#546
  • Loading branch information
lwshang authored Jan 15, 2025
1 parent a238231 commit d5a5127
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

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

6 changes: 6 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@

# Changelog

## 2025-01-15

### Candid 0.10.12

* Implement `CandidType` for `std::marker::PhantomData`.

## 2024-12-10

### Candid 0.10.11
Expand Down
2 changes: 1 addition & 1 deletion rust/candid/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "candid"
version = "0.10.11"
version = "0.10.12"
edition = "2021"
rust-version.workspace = true
authors = ["DFINITY Team"]
Expand Down
16 changes: 16 additions & 0 deletions rust/candid/src/types/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,22 @@ where
}
}

impl<T> CandidType for std::marker::PhantomData<T>
where
T: CandidType,
{
fn _ty() -> Type {
T::ty()
}
fn idl_serialize<S>(&self, _: S) -> Result<(), S::Error>
where
S: Serializer,
{
use serde::ser::Error;
Err(S::Error::custom("`PhantomData` cannot be serialized"))
}
}

macro_rules! tuple_impls {
($($len:expr => ($($n:tt $name:ident)+))+) => {
$(
Expand Down
4 changes: 4 additions & 0 deletions rust/candid/tests/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ fn test_primitive() {
get_type(&[0, 1, 2, 3]),
TypeInner::Vec(TypeInner::Int32.into()).into()
);
assert_eq!(
get_type(&std::marker::PhantomData::<u32>),
TypeInner::Nat32.into()
);
}

#[test]
Expand Down

0 comments on commit d5a5127

Please sign in to comment.