Skip to content

Commit

Permalink
update starcoin-vm-types
Browse files Browse the repository at this point in the history
  • Loading branch information
nkysg committed Sep 2, 2024
1 parent ae0970c commit c23f890
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
2 changes: 1 addition & 1 deletion vm/types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ vm = { workspace = true }
bytes = { workspace = true }
strum = { workspace = true }
strum_macros = { workspace = true }
bcs = { workspace = true }

[dev-dependencies]
proptest = { workspace = true }
proptest-derive = { workspace = true }
starcoin-crypto = { workspace = true }
starcoin-time-service = { workspace = true }
# vm = { workspace = true }
starcoin-gas-algebra-ext = { workspace = true, features = ["testing"] }

[features]
Expand Down
2 changes: 2 additions & 0 deletions vm/types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ mod language_storage_ext;

pub mod account_address;
pub mod gas_schedule;

pub mod move_any;
pub mod location {
pub use move_ir_types::location::Loc;
}
Expand Down
42 changes: 42 additions & 0 deletions vm/types/src/move_any.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright © Aptos Foundation

use anyhow::bail;
use serde::{de::DeserializeOwned, Deserialize, Serialize};

/// Rust representation of the Move Any type
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
pub struct Any {
pub type_name: String,
#[serde(with = "serde_bytes")]
pub data: Vec<u8>,
}

impl Any {
pub fn pack<T: Serialize>(move_name: &str, x: T) -> Any {
Any {
type_name: move_name.to_string(),
data: bcs::to_bytes(&x).unwrap(),
}
}

pub fn unpack<T: DeserializeOwned>(move_name: &str, x: Any) -> anyhow::Result<T> {
let Any { type_name, data } = x;
if type_name == move_name {
let y = bcs::from_bytes::<T>(&data)?;
Ok(y)
} else {
bail!("type mismatch")
}
}
}

pub trait AsMoveAny: Serialize {
const MOVE_TYPE_NAME: &'static str;

fn as_move_any(&self) -> Any
where
Self: Sized,
{
Any::pack(Self::MOVE_TYPE_NAME, self)
}
}

0 comments on commit c23f890

Please sign in to comment.