-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[framework] Add copyable_any_map::AnyMap
- Loading branch information
1 parent
4024942
commit 3013155
Showing
5 changed files
with
278 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,225 @@ | ||
|
||
<a id="0x1_any_map"></a> | ||
|
||
# Module `0x1::any_map` | ||
|
||
|
||
|
||
- [Struct `AnyMap`](#0x1_any_map_AnyMap) | ||
- [Constants](#@Constants_0) | ||
- [Function `new`](#0x1_any_map_new) | ||
- [Function `add`](#0x1_any_map_add) | ||
- [Function `get_copy`](#0x1_any_map_get_copy) | ||
- [Function `remove`](#0x1_any_map_remove) | ||
- [Function `remove_if_present`](#0x1_any_map_remove_if_present) | ||
- [Function `length`](#0x1_any_map_length) | ||
|
||
|
||
<pre><code><b>use</b> <a href="../../move-stdlib/doc/bcs.md#0x1_bcs">0x1::bcs</a>; | ||
<b>use</b> <a href="from_bcs.md#0x1_from_bcs">0x1::from_bcs</a>; | ||
<b>use</b> <a href="../../move-stdlib/doc/option.md#0x1_option">0x1::option</a>; | ||
<b>use</b> <a href="ordered_map.md#0x1_ordered_map">0x1::ordered_map</a>; | ||
<b>use</b> <a href="../../move-stdlib/doc/string.md#0x1_string">0x1::string</a>; | ||
<b>use</b> <a href="type_info.md#0x1_type_info">0x1::type_info</a>; | ||
<b>use</b> <a href="../../move-stdlib/doc/vector.md#0x1_vector">0x1::vector</a>; | ||
</code></pre> | ||
|
||
|
||
|
||
<a id="0x1_any_map_AnyMap"></a> | ||
|
||
## Struct `AnyMap` | ||
|
||
|
||
|
||
<pre><code><b>struct</b> <a href="any_map.md#0x1_any_map_AnyMap">AnyMap</a> <b>has</b> drop, store | ||
</code></pre> | ||
|
||
|
||
|
||
<details> | ||
<summary>Fields</summary> | ||
|
||
|
||
<dl> | ||
<dt> | ||
<code>entries: <a href="ordered_map.md#0x1_ordered_map_OrderedMap">ordered_map::OrderedMap</a><<a href="../../move-stdlib/doc/string.md#0x1_string_String">string::String</a>, <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a><u8>></code> | ||
</dt> | ||
<dd> | ||
|
||
</dd> | ||
</dl> | ||
|
||
|
||
</details> | ||
|
||
<a id="@Constants_0"></a> | ||
|
||
## Constants | ||
|
||
|
||
<a id="0x1_any_map_ETYPE_MISMATCH"></a> | ||
|
||
The type provided for <code>unpack</code> is not the same as was given for <code>pack</code>. | ||
|
||
|
||
<pre><code><b>const</b> <a href="any_map.md#0x1_any_map_ETYPE_MISMATCH">ETYPE_MISMATCH</a>: u64 = 1; | ||
</code></pre> | ||
|
||
|
||
|
||
<a id="0x1_any_map_new"></a> | ||
|
||
## Function `new` | ||
|
||
|
||
|
||
<pre><code><b>public</b> <b>fun</b> <a href="any_map.md#0x1_any_map_new">new</a>(): <a href="any_map.md#0x1_any_map_AnyMap">any_map::AnyMap</a> | ||
</code></pre> | ||
|
||
|
||
|
||
<details> | ||
<summary>Implementation</summary> | ||
|
||
|
||
<pre><code><b>public</b> <b>fun</b> <a href="any_map.md#0x1_any_map_new">new</a>(): <a href="any_map.md#0x1_any_map_AnyMap">AnyMap</a> { | ||
<a href="any_map.md#0x1_any_map_AnyMap">AnyMap</a> { | ||
entries: <a href="ordered_map.md#0x1_ordered_map_new">ordered_map::new</a>(), | ||
} | ||
} | ||
</code></pre> | ||
|
||
|
||
|
||
</details> | ||
|
||
<a id="0x1_any_map_add"></a> | ||
|
||
## Function `add` | ||
|
||
|
||
|
||
<pre><code><b>public</b> <b>fun</b> <a href="any_map.md#0x1_any_map_add">add</a><T: drop, store>(self: &<b>mut</b> <a href="any_map.md#0x1_any_map_AnyMap">any_map::AnyMap</a>, x: T) | ||
</code></pre> | ||
|
||
|
||
|
||
<details> | ||
<summary>Implementation</summary> | ||
|
||
|
||
<pre><code><b>public</b> <b>fun</b> <a href="any_map.md#0x1_any_map_add">add</a><T: drop + store>(self: &<b>mut</b> <a href="any_map.md#0x1_any_map_AnyMap">AnyMap</a>, x: T) { | ||
self.entries.<a href="any_map.md#0x1_any_map_add">add</a>(<a href="type_info.md#0x1_type_info_type_name">type_info::type_name</a><T>(), to_bytes(&x)); | ||
} | ||
</code></pre> | ||
|
||
|
||
|
||
</details> | ||
|
||
<a id="0x1_any_map_get_copy"></a> | ||
|
||
## Function `get_copy` | ||
|
||
|
||
|
||
<pre><code><b>public</b> <b>fun</b> <a href="any_map.md#0x1_any_map_get_copy">get_copy</a><T: <b>copy</b>, drop, store>(self: &<a href="any_map.md#0x1_any_map_AnyMap">any_map::AnyMap</a>): T | ||
</code></pre> | ||
|
||
|
||
|
||
<details> | ||
<summary>Implementation</summary> | ||
|
||
|
||
<pre><code><b>public</b> <b>fun</b> <a href="any_map.md#0x1_any_map_get_copy">get_copy</a><T: <b>copy</b> + drop + store>(self: &<a href="any_map.md#0x1_any_map_AnyMap">AnyMap</a>): T { | ||
<b>let</b> data = self.entries.borrow(&<a href="type_info.md#0x1_type_info_type_name">type_info::type_name</a><T>()); | ||
<a href="from_bcs.md#0x1_from_bcs_from_bytes">from_bcs::from_bytes</a><T>(<a href="../../move-stdlib/doc/vector.md#0x1_vector_slice">vector::slice</a>(data, 0, <a href="../../move-stdlib/doc/vector.md#0x1_vector_length">vector::length</a>(data))) | ||
} | ||
</code></pre> | ||
|
||
|
||
|
||
</details> | ||
|
||
<a id="0x1_any_map_remove"></a> | ||
|
||
## Function `remove` | ||
|
||
|
||
|
||
<pre><code><b>public</b> <b>fun</b> <a href="any_map.md#0x1_any_map_remove">remove</a><T>(self: &<b>mut</b> <a href="any_map.md#0x1_any_map_AnyMap">any_map::AnyMap</a>): T | ||
</code></pre> | ||
|
||
|
||
|
||
<details> | ||
<summary>Implementation</summary> | ||
|
||
|
||
<pre><code><b>public</b> <b>fun</b> <a href="any_map.md#0x1_any_map_remove">remove</a><T>(self: &<b>mut</b> <a href="any_map.md#0x1_any_map_AnyMap">AnyMap</a>): T { | ||
<b>let</b> data = self.entries.<a href="any_map.md#0x1_any_map_remove">remove</a>(&<a href="type_info.md#0x1_type_info_type_name">type_info::type_name</a><T>()); | ||
<a href="from_bcs.md#0x1_from_bcs_from_bytes">from_bcs::from_bytes</a><T>(data) | ||
} | ||
</code></pre> | ||
|
||
|
||
|
||
</details> | ||
|
||
<a id="0x1_any_map_remove_if_present"></a> | ||
|
||
## Function `remove_if_present` | ||
|
||
|
||
|
||
<pre><code><b>public</b> <b>fun</b> <a href="any_map.md#0x1_any_map_remove_if_present">remove_if_present</a><T>(self: &<b>mut</b> <a href="any_map.md#0x1_any_map_AnyMap">any_map::AnyMap</a>): <a href="../../move-stdlib/doc/option.md#0x1_option_Option">option::Option</a><T> | ||
</code></pre> | ||
|
||
|
||
|
||
<details> | ||
<summary>Implementation</summary> | ||
|
||
|
||
<pre><code><b>public</b> <b>fun</b> <a href="any_map.md#0x1_any_map_remove_if_present">remove_if_present</a><T>(self: &<b>mut</b> <a href="any_map.md#0x1_any_map_AnyMap">AnyMap</a>): <a href="../../move-stdlib/doc/option.md#0x1_option_Option">option::Option</a><T> { | ||
<b>let</b> iter = self.entries.find(&<a href="type_info.md#0x1_type_info_type_name">type_info::type_name</a><T>()); | ||
<b>if</b> (iter.iter_is_end(&self.entries)) { | ||
<a href="../../move-stdlib/doc/option.md#0x1_option_none">option::none</a>() | ||
} <b>else</b> { | ||
<a href="../../move-stdlib/doc/option.md#0x1_option_some">option::some</a>(<a href="from_bcs.md#0x1_from_bcs_from_bytes">from_bcs::from_bytes</a><T>(iter.iter_remove(&<b>mut</b> self.entries))) | ||
} | ||
} | ||
</code></pre> | ||
|
||
|
||
|
||
</details> | ||
|
||
<a id="0x1_any_map_length"></a> | ||
|
||
## Function `length` | ||
|
||
|
||
|
||
<pre><code><b>public</b> <b>fun</b> <a href="any_map.md#0x1_any_map_length">length</a>(self: &<a href="any_map.md#0x1_any_map_AnyMap">any_map::AnyMap</a>): u64 | ||
</code></pre> | ||
|
||
|
||
|
||
<details> | ||
<summary>Implementation</summary> | ||
|
||
|
||
<pre><code><b>public</b> <b>fun</b> <a href="any_map.md#0x1_any_map_length">length</a>(self: &<a href="any_map.md#0x1_any_map_AnyMap">AnyMap</a>): u64 { | ||
self.entries.<a href="any_map.md#0x1_any_map_length">length</a>() | ||
} | ||
</code></pre> | ||
|
||
|
||
|
||
</details> | ||
|
||
|
||
[move-book]: https://aptos.dev/move/book/SUMMARY |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
aptos-move/framework/aptos-stdlib/sources/data_structures/any_map.move
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
module aptos_std::any_map { | ||
use std::bcs::to_bytes; | ||
use std::option; | ||
use std::string::String; | ||
use std::vector; | ||
use aptos_std::from_bcs; | ||
use aptos_std::ordered_map::{Self, OrderedMap}; | ||
use aptos_std::type_info; | ||
|
||
/// The type provided for `unpack` is not the same as was given for `pack`. | ||
const ETYPE_MISMATCH: u64 = 1; | ||
|
||
struct AnyMap has drop, store { | ||
entries: OrderedMap<String, vector<u8>>, | ||
} | ||
|
||
public fun new(): AnyMap { | ||
AnyMap { | ||
entries: ordered_map::new(), | ||
} | ||
} | ||
|
||
public fun add<T: drop + store>(self: &mut AnyMap, x: T) { | ||
self.entries.add(type_info::type_name<T>(), to_bytes(&x)); | ||
} | ||
|
||
public fun get_copy<T: copy + drop + store>(self: &AnyMap): T { | ||
let data = self.entries.borrow(&type_info::type_name<T>()); | ||
from_bcs::from_bytes<T>(vector::slice(data, 0, vector::length(data))) | ||
} | ||
|
||
public fun remove<T>(self: &mut AnyMap): T { | ||
let data = self.entries.remove(&type_info::type_name<T>()); | ||
from_bcs::from_bytes<T>(data) | ||
} | ||
|
||
public fun remove_if_present<T>(self: &mut AnyMap): option::Option<T> { | ||
let iter = self.entries.find(&type_info::type_name<T>()); | ||
if (iter.iter_is_end(&self.entries)) { | ||
option::none() | ||
} else { | ||
option::some(from_bcs::from_bytes<T>(iter.iter_remove(&mut self.entries))) | ||
} | ||
} | ||
|
||
public fun length(self: &AnyMap): u64 { | ||
self.entries.length() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters