sui_types/
layout_resolver.rs1use crate::error::{SuiError, SuiErrorKind};
5use move_bytecode_utils::{layout::TypeLayoutBuilder, module_cache::GetModule};
6use move_core_types::{
7 annotated_value as A,
8 language_storage::{StructTag, TypeTag},
9};
10
11pub trait LayoutResolver {
12 fn get_annotated_layout(
13 &mut self,
14 struct_tag: &StructTag,
15 ) -> Result<A::MoveDatatypeLayout, SuiError>;
16}
17
18pub fn get_layout_from_struct_tag(
19 struct_tag: StructTag,
20 resolver: &impl GetModule,
21) -> Result<A::MoveDatatypeLayout, SuiError> {
22 let type_ = TypeTag::Struct(Box::new(struct_tag));
23 let layout = TypeLayoutBuilder::build_with_types(&type_, resolver).map_err(|e| {
24 SuiErrorKind::ObjectSerializationError {
25 error: e.to_string(),
26 }
27 })?;
28 match layout {
29 A::MoveTypeLayout::Struct(l) => Ok(A::MoveDatatypeLayout::Struct(l)),
30 A::MoveTypeLayout::Enum(e) => Ok(A::MoveDatatypeLayout::Enum(e)),
31 _ => {
32 unreachable!(
33 "We called get_layout_from_struct_tag on a datatype, should get a datatype layout"
34 )
35 }
36 }
37}
38
39pub fn into_struct_layout(layout: A::MoveDatatypeLayout) -> Result<A::MoveStructLayout, SuiError> {
40 match layout {
41 A::MoveDatatypeLayout::Struct(s) => Ok(*s),
42 A::MoveDatatypeLayout::Enum(e) => Err(SuiErrorKind::ObjectSerializationError {
43 error: format!("Expected struct layout but got an enum {e:?}"),
44 }
45 .into()),
46 }
47}