iota_sdk_move_types/
macros.rs1macro_rules! impl_try_from_object {
29 ($ty:ident, $is_fn:ident $(,)?) => {
30 #[cfg(feature = "serde")]
31 #[doc = concat!(
32 "Decode a [`",
33 stringify!($ty),
34 "`] from an on-chain object, validating that the object's Move type tag matches."
35 )]
36 impl TryFrom<&::iota_types::Object> for $ty {
37 type Error = $crate::FromObjectError;
38
39 fn try_from(object: &::iota_types::Object) -> Result<Self, Self::Error> {
40 let move_struct = object
41 .as_opt_struct()
42 .ok_or($crate::FromObjectError::NotAMoveStruct)?;
43 if !move_struct.object_type().$is_fn() {
44 return Err($crate::FromObjectError::WrongType);
45 }
46 ::bcs::from_bytes(move_struct.contents()).map_err($crate::FromObjectError::Bcs)
47 }
48 }
49 };
50 ($ty:ident $(,)?) => {
51 ::paste::paste! {
52 impl_try_from_object!($ty, [< is_ $ty:snake >]);
53 }
54 };
55}
56
57macro_rules! impl_try_from_object_generic {
64 ($ty:ident<$param:ident>, $is_fn:ident $(,)?) => {
65 #[cfg(feature = "serde")]
66 impl<$param> $ty<$param>
67 where
68 $param: ::serde::de::DeserializeOwned,
69 {
70 #[doc = concat!(
71 "Decode a [`",
72 stringify!($ty),
73 "`] from an on-chain object, validating its Move type tag and that its type parameter equals `type_param`.\n\nEscape hatch for type parameters only known at runtime; nothing ties `type_param` to `",
74 stringify!($param),
75 "`. Prefer the `TryFrom` impl when the type parameter is known at compile time."
76 )]
77 pub fn try_from_object_with_type(
78 object: &::iota_types::Object,
79 type_param: &::iota_types::TypeTag,
80 ) -> Result<Self, $crate::FromObjectError> {
81 let move_struct = object
82 .as_opt_struct()
83 .ok_or($crate::FromObjectError::NotAMoveStruct)?;
84 let tag = move_struct.struct_tag();
85 if !tag.$is_fn() || tag.type_params() != ::core::slice::from_ref(type_param) {
86 return Err($crate::FromObjectError::WrongType);
87 }
88 ::bcs::from_bytes(move_struct.contents()).map_err($crate::FromObjectError::Bcs)
89 }
90 }
91
92 #[cfg(feature = "serde")]
93 #[doc = concat!(
94 "Decode a [`",
95 stringify!($ty),
96 "`] from an on-chain object, validating its full Move type tag including the type parameter."
97 )]
98 impl<$param> TryFrom<&::iota_types::Object> for $ty<$param>
99 where
100 $param: ::serde::de::DeserializeOwned + $crate::MoveType,
101 {
102 type Error = $crate::FromObjectError;
103
104 fn try_from(object: &::iota_types::Object) -> Result<Self, Self::Error> {
105 Self::try_from_object_with_type(object, &<$param as $crate::MoveType>::type_tag())
106 }
107 }
108 };
109 ($ty:ident<$param:ident> $(,)?) => {
110 ::paste::paste! {
111 impl_try_from_object_generic!($ty<$param>, [< is_ $ty:snake >]);
112 }
113 };
114}