sui_adapter_latest/
type_layout_resolver.rsuse crate::programmable_transactions::context::load_type_from_struct;
use crate::programmable_transactions::linkage_view::LinkageView;
use move_core_types::account_address::AccountAddress;
use move_core_types::annotated_value as A;
use move_core_types::language_storage::StructTag;
use move_core_types::resolver::ResourceResolver;
use move_vm_runtime::move_vm::MoveVM;
use sui_types::base_types::ObjectID;
use sui_types::error::SuiResult;
use sui_types::execution::TypeLayoutStore;
use sui_types::storage::{BackingPackageStore, PackageObject};
use sui_types::{error::SuiError, layout_resolver::LayoutResolver};
pub struct TypeLayoutResolver<'state, 'vm> {
vm: &'vm MoveVM,
linkage_view: LinkageView<'state>,
}
struct NullSuiResolver<'state>(Box<dyn TypeLayoutStore + 'state>);
impl<'state, 'vm> TypeLayoutResolver<'state, 'vm> {
pub fn new(vm: &'vm MoveVM, state_view: Box<dyn TypeLayoutStore + 'state>) -> Self {
let linkage_view = LinkageView::new(Box::new(NullSuiResolver(state_view)));
Self { vm, linkage_view }
}
}
impl LayoutResolver for TypeLayoutResolver<'_, '_> {
fn get_annotated_layout(
&mut self,
struct_tag: &StructTag,
) -> Result<A::MoveDatatypeLayout, SuiError> {
let Ok(ty) = load_type_from_struct(self.vm, &mut self.linkage_view, &[], struct_tag) else {
return Err(SuiError::FailObjectLayout {
st: format!("{}", struct_tag),
});
};
let layout = self.vm.get_runtime().type_to_fully_annotated_layout(&ty);
match layout {
Ok(A::MoveTypeLayout::Struct(s)) => Ok(A::MoveDatatypeLayout::Struct(s)),
Ok(A::MoveTypeLayout::Enum(e)) => Ok(A::MoveDatatypeLayout::Enum(e)),
_ => Err(SuiError::FailObjectLayout {
st: format!("{}", struct_tag),
}),
}
}
}
impl BackingPackageStore for NullSuiResolver<'_> {
fn get_package_object(&self, package_id: &ObjectID) -> SuiResult<Option<PackageObject>> {
self.0.get_package_object(package_id)
}
}
impl ResourceResolver for NullSuiResolver<'_> {
type Error = SuiError;
fn get_resource(
&self,
_address: &AccountAddress,
_typ: &StructTag,
) -> Result<Option<Vec<u8>>, Self::Error> {
Ok(None)
}
}