|
| 1 | + |
| 2 | +<a id="0x1_box_or_inline"></a> |
| 3 | + |
| 4 | +# Module `0x1::box_or_inline` |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | +- [Enum `BoxOrInline`](#0x1_box_or_inline_BoxOrInline) |
| 9 | +- [Struct `Dummy`](#0x1_box_or_inline_Dummy) |
| 10 | +- [Constants](#@Constants_0) |
| 11 | +- [Function `new_inline`](#0x1_box_or_inline_new_inline) |
| 12 | +- [Function `new_box`](#0x1_box_or_inline_new_box) |
| 13 | +- [Function `borrow`](#0x1_box_or_inline_borrow) |
| 14 | +- [Function `borrow_mut`](#0x1_box_or_inline_borrow_mut) |
| 15 | +- [Function `destroy`](#0x1_box_or_inline_destroy) |
| 16 | +- [Function `move_to_inline`](#0x1_box_or_inline_move_to_inline) |
| 17 | +- [Function `move_to_box`](#0x1_box_or_inline_move_to_box) |
| 18 | + |
| 19 | + |
| 20 | +<pre><code><b>use</b> <a href="../../move-stdlib/doc/mem.md#0x1_mem">0x1::mem</a>; |
| 21 | +<b>use</b> <a href="table.md#0x1_table">0x1::table</a>; |
| 22 | +</code></pre> |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | +<a id="0x1_box_or_inline_BoxOrInline"></a> |
| 27 | + |
| 28 | +## Enum `BoxOrInline` |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | +<pre><code>enum <a href="box_or_inline.md#0x1_box_or_inline_BoxOrInline">BoxOrInline</a><T> <b>has</b> store |
| 33 | +</code></pre> |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | +<details> |
| 38 | +<summary>Variants</summary> |
| 39 | + |
| 40 | + |
| 41 | +<details> |
| 42 | +<summary>Inline</summary> |
| 43 | + |
| 44 | + |
| 45 | +<details> |
| 46 | +<summary>Fields</summary> |
| 47 | + |
| 48 | + |
| 49 | +<dl> |
| 50 | +<dt> |
| 51 | +<code>value: T</code> |
| 52 | +</dt> |
| 53 | +<dd> |
| 54 | + |
| 55 | +</dd> |
| 56 | +</dl> |
| 57 | + |
| 58 | + |
| 59 | +</details> |
| 60 | + |
| 61 | +</details> |
| 62 | + |
| 63 | +<details> |
| 64 | +<summary>BoxInTable</summary> |
| 65 | + |
| 66 | + |
| 67 | +<details> |
| 68 | +<summary>Fields</summary> |
| 69 | + |
| 70 | + |
| 71 | +<dl> |
| 72 | +<dt> |
| 73 | +<code><a href="table.md#0x1_table">table</a>: <a href="table.md#0x1_table_Table">table::Table</a><bool, T></code> |
| 74 | +</dt> |
| 75 | +<dd> |
| 76 | + |
| 77 | +</dd> |
| 78 | +</dl> |
| 79 | + |
| 80 | + |
| 81 | +</details> |
| 82 | + |
| 83 | +</details> |
| 84 | + |
| 85 | +</details> |
| 86 | + |
| 87 | +<a id="0x1_box_or_inline_Dummy"></a> |
| 88 | + |
| 89 | +## Struct `Dummy` |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | +<pre><code><b>struct</b> <a href="box_or_inline.md#0x1_box_or_inline_Dummy">Dummy</a> <b>has</b> store |
| 94 | +</code></pre> |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | +<details> |
| 99 | +<summary>Fields</summary> |
| 100 | + |
| 101 | + |
| 102 | +<dl> |
| 103 | +<dt> |
| 104 | +<code>dummy_field: bool</code> |
| 105 | +</dt> |
| 106 | +<dd> |
| 107 | + |
| 108 | +</dd> |
| 109 | +</dl> |
| 110 | + |
| 111 | + |
| 112 | +</details> |
| 113 | + |
| 114 | +<a id="@Constants_0"></a> |
| 115 | + |
| 116 | +## Constants |
| 117 | + |
| 118 | + |
| 119 | +<a id="0x1_box_or_inline_ONLY_KEY"></a> |
| 120 | + |
| 121 | + |
| 122 | + |
| 123 | +<pre><code><b>const</b> <a href="box_or_inline.md#0x1_box_or_inline_ONLY_KEY">ONLY_KEY</a>: bool = <b>true</b>; |
| 124 | +</code></pre> |
| 125 | + |
| 126 | + |
| 127 | + |
| 128 | +<a id="0x1_box_or_inline_new_inline"></a> |
| 129 | + |
| 130 | +## Function `new_inline` |
| 131 | + |
| 132 | + |
| 133 | + |
| 134 | +<pre><code><b>public</b> <b>fun</b> <a href="box_or_inline.md#0x1_box_or_inline_new_inline">new_inline</a><T: store>(value: T): <a href="box_or_inline.md#0x1_box_or_inline_BoxOrInline">box_or_inline::BoxOrInline</a><T> |
| 135 | +</code></pre> |
| 136 | + |
| 137 | + |
| 138 | + |
| 139 | +<details> |
| 140 | +<summary>Implementation</summary> |
| 141 | + |
| 142 | + |
| 143 | +<pre><code><b>public</b> <b>fun</b> <a href="box_or_inline.md#0x1_box_or_inline_new_inline">new_inline</a><T: store>(value: T): <a href="box_or_inline.md#0x1_box_or_inline_BoxOrInline">BoxOrInline</a><T> { |
| 144 | + BoxOrInline::Inline { value } |
| 145 | +} |
| 146 | +</code></pre> |
| 147 | + |
| 148 | + |
| 149 | + |
| 150 | +</details> |
| 151 | + |
| 152 | +<a id="0x1_box_or_inline_new_box"></a> |
| 153 | + |
| 154 | +## Function `new_box` |
| 155 | + |
| 156 | + |
| 157 | + |
| 158 | +<pre><code><b>public</b> <b>fun</b> <a href="box_or_inline.md#0x1_box_or_inline_new_box">new_box</a><T: store>(value: T): <a href="box_or_inline.md#0x1_box_or_inline_BoxOrInline">box_or_inline::BoxOrInline</a><T> |
| 159 | +</code></pre> |
| 160 | + |
| 161 | + |
| 162 | + |
| 163 | +<details> |
| 164 | +<summary>Implementation</summary> |
| 165 | + |
| 166 | + |
| 167 | +<pre><code><b>public</b> <b>fun</b> <a href="box_or_inline.md#0x1_box_or_inline_new_box">new_box</a><T: store>(value: T): <a href="box_or_inline.md#0x1_box_or_inline_BoxOrInline">BoxOrInline</a><T> { |
| 168 | + <b>let</b> <a href="table.md#0x1_table">table</a> = <a href="table.md#0x1_table_new">table::new</a>(); |
| 169 | + <a href="table.md#0x1_table">table</a>.add(<a href="box_or_inline.md#0x1_box_or_inline_ONLY_KEY">ONLY_KEY</a>, value); |
| 170 | + BoxOrInline::BoxInTable { <a href="table.md#0x1_table">table</a> } |
| 171 | +} |
| 172 | +</code></pre> |
| 173 | + |
| 174 | + |
| 175 | + |
| 176 | +</details> |
| 177 | + |
| 178 | +<a id="0x1_box_or_inline_borrow"></a> |
| 179 | + |
| 180 | +## Function `borrow` |
| 181 | + |
| 182 | + |
| 183 | + |
| 184 | +<pre><code><b>public</b> <b>fun</b> <a href="box_or_inline.md#0x1_box_or_inline_borrow">borrow</a><T: store>(self: &<a href="box_or_inline.md#0x1_box_or_inline_BoxOrInline">box_or_inline::BoxOrInline</a><T>): &T |
| 185 | +</code></pre> |
| 186 | + |
| 187 | + |
| 188 | + |
| 189 | +<details> |
| 190 | +<summary>Implementation</summary> |
| 191 | + |
| 192 | + |
| 193 | +<pre><code><b>public</b> <b>fun</b> <a href="box_or_inline.md#0x1_box_or_inline_borrow">borrow</a><T: store>(self: &<a href="box_or_inline.md#0x1_box_or_inline_BoxOrInline">BoxOrInline</a><T>): &T { |
| 194 | + match (self) { |
| 195 | + BoxOrInline::Inline { value } => value, |
| 196 | + BoxOrInline::BoxInTable { <a href="table.md#0x1_table">table</a> } => <a href="table.md#0x1_table">table</a>.<a href="box_or_inline.md#0x1_box_or_inline_borrow">borrow</a>(<a href="box_or_inline.md#0x1_box_or_inline_ONLY_KEY">ONLY_KEY</a>), |
| 197 | + } |
| 198 | +} |
| 199 | +</code></pre> |
| 200 | + |
| 201 | + |
| 202 | + |
| 203 | +</details> |
| 204 | + |
| 205 | +<a id="0x1_box_or_inline_borrow_mut"></a> |
| 206 | + |
| 207 | +## Function `borrow_mut` |
| 208 | + |
| 209 | + |
| 210 | + |
| 211 | +<pre><code><b>public</b> <b>fun</b> <a href="box_or_inline.md#0x1_box_or_inline_borrow_mut">borrow_mut</a><T: store>(self: &<b>mut</b> <a href="box_or_inline.md#0x1_box_or_inline_BoxOrInline">box_or_inline::BoxOrInline</a><T>): &<b>mut</b> T |
| 212 | +</code></pre> |
| 213 | + |
| 214 | + |
| 215 | + |
| 216 | +<details> |
| 217 | +<summary>Implementation</summary> |
| 218 | + |
| 219 | + |
| 220 | +<pre><code><b>public</b> <b>fun</b> <a href="box_or_inline.md#0x1_box_or_inline_borrow_mut">borrow_mut</a><T: store>(self: &<b>mut</b> <a href="box_or_inline.md#0x1_box_or_inline_BoxOrInline">BoxOrInline</a><T>): &<b>mut</b> T { |
| 221 | + match (self) { |
| 222 | + BoxOrInline::Inline { value } => value, |
| 223 | + BoxOrInline::BoxInTable { <a href="table.md#0x1_table">table</a> } => <a href="table.md#0x1_table">table</a>.<a href="box_or_inline.md#0x1_box_or_inline_borrow_mut">borrow_mut</a>(<a href="box_or_inline.md#0x1_box_or_inline_ONLY_KEY">ONLY_KEY</a>), |
| 224 | + } |
| 225 | +} |
| 226 | +</code></pre> |
| 227 | + |
| 228 | + |
| 229 | + |
| 230 | +</details> |
| 231 | + |
| 232 | +<a id="0x1_box_or_inline_destroy"></a> |
| 233 | + |
| 234 | +## Function `destroy` |
| 235 | + |
| 236 | + |
| 237 | + |
| 238 | +<pre><code><b>public</b> <b>fun</b> <a href="box_or_inline.md#0x1_box_or_inline_destroy">destroy</a><T: store>(self: <a href="box_or_inline.md#0x1_box_or_inline_BoxOrInline">box_or_inline::BoxOrInline</a><T>): T |
| 239 | +</code></pre> |
| 240 | + |
| 241 | + |
| 242 | + |
| 243 | +<details> |
| 244 | +<summary>Implementation</summary> |
| 245 | + |
| 246 | + |
| 247 | +<pre><code><b>public</b> <b>fun</b> <a href="box_or_inline.md#0x1_box_or_inline_destroy">destroy</a><T: store>(self: <a href="box_or_inline.md#0x1_box_or_inline_BoxOrInline">BoxOrInline</a><T>): T { |
| 248 | + match (self) { |
| 249 | + BoxOrInline::Inline { value } => value, |
| 250 | + BoxOrInline::BoxInTable { <a href="table.md#0x1_table">table</a> } => { |
| 251 | + <b>let</b> value = <a href="table.md#0x1_table">table</a>.remove(<a href="box_or_inline.md#0x1_box_or_inline_ONLY_KEY">ONLY_KEY</a>); |
| 252 | + <a href="table.md#0x1_table">table</a>.destroy_known_empty_unsafe(); |
| 253 | + value |
| 254 | + }, |
| 255 | + } |
| 256 | +} |
| 257 | +</code></pre> |
| 258 | + |
| 259 | + |
| 260 | + |
| 261 | +</details> |
| 262 | + |
| 263 | +<a id="0x1_box_or_inline_move_to_inline"></a> |
| 264 | + |
| 265 | +## Function `move_to_inline` |
| 266 | + |
| 267 | + |
| 268 | + |
| 269 | +<pre><code><b>public</b> <b>fun</b> <a href="box_or_inline.md#0x1_box_or_inline_move_to_inline">move_to_inline</a><T: store>(self: &<b>mut</b> <a href="box_or_inline.md#0x1_box_or_inline_BoxOrInline">box_or_inline::BoxOrInline</a><T>) |
| 270 | +</code></pre> |
| 271 | + |
| 272 | + |
| 273 | + |
| 274 | +<details> |
| 275 | +<summary>Implementation</summary> |
| 276 | + |
| 277 | + |
| 278 | +<pre><code><b>public</b> <b>fun</b> <a href="box_or_inline.md#0x1_box_or_inline_move_to_inline">move_to_inline</a><T: store>(self: &<b>mut</b> <a href="box_or_inline.md#0x1_box_or_inline_BoxOrInline">BoxOrInline</a><T>) { |
| 279 | + match (self) { |
| 280 | + BoxOrInline::Inline { value: _ } => {}, |
| 281 | + BoxOrInline::BoxInTable { <a href="table.md#0x1_table">table</a> } => { |
| 282 | + <b>let</b> value = <a href="table.md#0x1_table">table</a>.remove(<a href="box_or_inline.md#0x1_box_or_inline_ONLY_KEY">ONLY_KEY</a>); |
| 283 | + <b>let</b> BoxOrInline::BoxInTable { <a href="table.md#0x1_table">table</a> } = <a href="../../move-stdlib/doc/mem.md#0x1_mem_replace">mem::replace</a>(self, BoxOrInline::Inline { value }); |
| 284 | + <a href="table.md#0x1_table">table</a>.destroy_known_empty_unsafe(); |
| 285 | + }, |
| 286 | + } |
| 287 | +} |
| 288 | +</code></pre> |
| 289 | + |
| 290 | + |
| 291 | + |
| 292 | +</details> |
| 293 | + |
| 294 | +<a id="0x1_box_or_inline_move_to_box"></a> |
| 295 | + |
| 296 | +## Function `move_to_box` |
| 297 | + |
| 298 | + |
| 299 | + |
| 300 | +<pre><code><b>public</b> <b>fun</b> <a href="box_or_inline.md#0x1_box_or_inline_move_to_box">move_to_box</a><T: store>(self: &<b>mut</b> <a href="box_or_inline.md#0x1_box_or_inline_BoxOrInline">box_or_inline::BoxOrInline</a><T>) |
| 301 | +</code></pre> |
| 302 | + |
| 303 | + |
| 304 | + |
| 305 | +<details> |
| 306 | +<summary>Implementation</summary> |
| 307 | + |
| 308 | + |
| 309 | +<pre><code><b>public</b> <b>fun</b> <a href="box_or_inline.md#0x1_box_or_inline_move_to_box">move_to_box</a><T: store>(self: &<b>mut</b> <a href="box_or_inline.md#0x1_box_or_inline_BoxOrInline">BoxOrInline</a><T>) { |
| 310 | + match (self) { |
| 311 | + BoxOrInline::Inline { value: _ } => { |
| 312 | + <b>let</b> BoxOrInline::Inline { value } = <a href="../../move-stdlib/doc/mem.md#0x1_mem_replace">mem::replace</a>(self, BoxOrInline::BoxInTable { <a href="table.md#0x1_table">table</a>: <a href="table.md#0x1_table_new">table::new</a>() }); |
| 313 | + self.<a href="table.md#0x1_table">table</a>.add(<a href="box_or_inline.md#0x1_box_or_inline_ONLY_KEY">ONLY_KEY</a>, value); |
| 314 | + }, |
| 315 | + BoxOrInline::BoxInTable { <a href="table.md#0x1_table">table</a>: _ } => {}, |
| 316 | + } |
| 317 | +} |
| 318 | +</code></pre> |
| 319 | + |
| 320 | + |
| 321 | + |
| 322 | +</details> |
| 323 | + |
| 324 | + |
| 325 | +[move-book]: https://aptos.dev/move/book/SUMMARY |
0 commit comments