sui_types/
collection_types.rs

1// Copyright (c) Mysten Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4use serde::{Deserialize, Serialize};
5
6use crate::base_types::ObjectID;
7use crate::id::UID;
8
9/// Rust version of the Move sui::vec_map::VecMap type
10#[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq)]
11pub struct VecMap<K, V> {
12    pub contents: Vec<Entry<K, V>>,
13}
14
15impl<K: PartialEq, V> VecMap<K, V> {
16    pub fn get(&self, key: &K) -> Option<&V> {
17        self.contents
18            .iter()
19            .find_map(|entry| (&entry.key == key).then_some(&entry.value))
20    }
21}
22
23/// Rust version of the Move sui::vec_map::Entry type
24#[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq)]
25pub struct Entry<K, V> {
26    pub key: K,
27    pub value: V,
28}
29
30/// Rust version of the Move sui::vec_set::VecSet type
31#[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq)]
32pub struct VecSet<T> {
33    pub contents: Vec<T>,
34}
35
36/// Rust version of the Move sui::table::Table type.
37#[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq)]
38pub struct TableVec {
39    pub contents: Table,
40}
41
42impl Default for TableVec {
43    fn default() -> Self {
44        TableVec {
45            contents: Table {
46                id: ObjectID::ZERO,
47                size: 0,
48            },
49        }
50    }
51}
52
53/// Rust version of the Move sui::table::Table type.
54#[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq)]
55pub struct Table {
56    pub id: ObjectID,
57    pub size: u64,
58}
59
60impl Default for Table {
61    fn default() -> Self {
62        Table {
63            id: ObjectID::ZERO,
64            size: 0,
65        }
66    }
67}
68
69/// Rust version of the Move sui::linked_table::LinkedTable type.
70#[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq)]
71pub struct LinkedTable<K> {
72    pub id: ObjectID,
73    pub size: u64,
74    pub head: Option<K>,
75    pub tail: Option<K>,
76}
77
78impl<K> Default for LinkedTable<K> {
79    fn default() -> Self {
80        LinkedTable {
81            id: ObjectID::ZERO,
82            size: 0,
83            head: None,
84            tail: None,
85        }
86    }
87}
88
89/// Rust version of the Move sui::linked_table::Node type.
90#[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq)]
91pub struct LinkedTableNode<K, V> {
92    pub prev: Option<K>,
93    pub next: Option<K>,
94    pub value: V,
95}
96
97/// Rust version of the Move sui::bag::Bag type.
98#[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq)]
99pub struct Bag {
100    pub id: UID,
101    pub size: u64,
102}
103
104impl Default for Bag {
105    fn default() -> Self {
106        Self {
107            id: UID::new(ObjectID::ZERO),
108            size: 0,
109        }
110    }
111}
112
113#[cfg(test)]
114mod tests {
115    use super::VecMap;
116
117    #[test]
118    fn test_vec_map() {
119        let vec_map = VecMap {
120            contents: vec![
121                ("key1".to_string(), "value1".to_string()),
122                ("key2".to_string(), "value2".to_string()),
123            ]
124            .into_iter()
125            .map(|(key, value)| super::Entry { key, value })
126            .collect(),
127        };
128
129        assert_eq!(
130            vec_map.get(&"key1".to_string()),
131            Some(&"value1".to_string())
132        );
133        assert_eq!(
134            vec_map.get(&"key2".to_string()),
135            Some(&"value2".to_string())
136        );
137        assert_eq!(vec_map.get(&"key3".to_string()), None);
138    }
139}