Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions crates/iota-sdk-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,12 @@ macro_rules! def_is_as_into_opt {
self.[< as_ $rename _opt >]().expect(&format!("not a {}", stringify!($variant)))
}

#[doc = "Converts this into a mut " $rename:snake " if it is a " $variant:snake " variant, or panics otherwise."]
#[inline]
pub fn [< as_ $rename _mut >](&mut self) -> &mut $inner {
self.[< as_ $rename _mut_opt >]().expect(&format!("not a {}", stringify!($variant)))
}

#[doc = "Converts this into a " $rename:snake " if it is a " $variant:snake " variant, or returns `None` otherwise."]
#[inline]
pub fn [< as_ $rename _opt >](&self) -> Option<&$inner> {
Expand All @@ -317,6 +323,17 @@ macro_rules! def_is_as_into_opt {
None
}
}

#[doc = "Converts this into a mut " $rename:snake " if it is a " $variant:snake " variant, or returns `None` otherwise."]
#[inline]
pub fn [< as_ $rename _mut_opt >](&mut self) -> Option<&mut $inner> {
#[allow(irrefutable_let_patterns)]
if let Self::$variant(inner) = self {
Some(inner)
} else {
None
}
}
}

$crate::def_is_as_into_opt!{@into $variant($rename) [$inner]}
Expand Down
21 changes: 21 additions & 0 deletions crates/iota-sdk-types/src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,27 @@ pub enum ObjectData {

impl ObjectData {
crate::def_is_as_into_opt!(Struct(MoveStruct), Package(MovePackage));

pub fn object_type(&self) -> Option<&MoveObjectType> {
match self {
Self::Struct(m) => Some(m.object_type()),
Self::Package(_) => None,
}
}

pub fn struct_tag(&self) -> Option<StructTag> {
Comment thread
Thoralf-M marked this conversation as resolved.
match self {
Self::Struct(m) => Some(m.struct_tag().clone()),
Self::Package(_) => None,
}
}

pub fn id(&self) -> ObjectId {
match self {
Self::Struct(v) => v.id(),
Self::Package(m) => m.id(),
}
}
}

/// A [`StructTag`] with optimized BCS serialization for object types.
Expand Down
Loading