af_move_type/otw.rs
1//! One-time-witness type.
2
3use serde::{Deserialize, Serialize};
4
5use crate::MoveStruct;
6
7/// Generic type signaling a one-time-witness type argument.
8///
9/// None of address, module and name are known at compile time, only that there are no type
10/// parameters
11///
12/// This is useful when calling associated methods on a move type that don't depend on the specific
13/// type argument.
14///
15/// # Examples
16/// ```
17/// use af_move_type::{MoveStruct, MoveType, otw::Otw};
18/// use serde::{Deserialize, Serialize};
19///
20/// #[derive(MoveStruct, Clone, Debug, Default, Deserialize, Serialize, PartialEq, Eq, Hash)]
21/// #[move_(address = "0x2", module = balance)]
22/// pub struct Balance<T: MoveType> {
23/// value: u64,
24/// _otw: std::marker::PhantomData<T>,
25/// }
26///
27/// // for compilation purposes
28/// impl<T: MoveType> std::fmt::Display for Balance<T> {
29/// fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
30/// todo!()
31/// }
32/// }
33///
34/// let address = "0x2".parse().unwrap();
35/// let module = "sui".parse().unwrap();
36/// let name = "SUI".parse().unwrap();
37/// let otw = Otw::type_(address, module, name);
38/// let usdc_type = Balance::<Otw>::type_(otw);
39/// ```
40#[derive(MoveStruct, Clone, Debug, Default, Deserialize, Serialize, PartialEq, Eq, Hash)]
41#[move_(crate = crate)]
42#[move_(nameless)]
43pub struct Otw {
44 dummy_field: bool,
45}
46
47impl Otw {
48 pub fn new() -> Self {
49 Default::default()
50 }
51}
52
53// This should never be used since OTWs are never instantiated.
54impl std::fmt::Display for Otw {
55 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
56 write!(f, "OTW")
57 }
58}