|
1 | 1 | // Copyright (c) Mysten Labs, Inc. |
2 | 2 | // SPDX-License-Identifier: Apache-2.0 |
3 | 3 |
|
4 | | -module book::buffer { |
5 | | - /// Error returned when the buffer is overflowed. |
6 | | - const EBufferOverflow: u64 = 0; |
7 | | - |
8 | | - /// The Buffer struct represents a growable buffer. |
9 | | - public struct Buffer { |
10 | | - data: vector<u8>, |
11 | | - expected_len: Option<u64> |
12 | | - } |
13 | | - |
14 | | - /// Creates a new empty buffer. |
15 | | - public fun new(): Buffer { |
16 | | - Buffer { data: vector[], expected_len: option::none() } |
17 | | - } |
18 | | - |
19 | | - /// Creates a new empty buffer with the specified capacity (in bytes). |
20 | | - /// If the buffer is overflowed, tx aborts with `EBufferOverflow`. |
21 | | - public fun alloc(len: u64): Buffer { |
22 | | - Buffer { data: vector[], expected_len: option::some(len) } |
23 | | - } |
24 | | - |
25 | | - /// Pushes the given data to the end of the buffer. |
26 | | - public fun push(self: &mut Buffer, data: vector<u8>) { |
27 | | - if (option::is_some(&self.expected_len)) { |
28 | | - let max_len = *option::borrow(&self.expected_len); |
29 | | - let future_len = self.len() + vector::length(&data); |
30 | | - assert!(future_len <= max_len, EBufferOverflow); |
31 | | - }; |
32 | | - |
33 | | - vector::append(&mut self.data, data) |
34 | | - } |
35 | | - |
36 | | - /// Unwraps the buffer and returns the underlying vector. |
37 | | - public fun unwrap(self: Buffer): vector<u8> { |
38 | | - let Buffer { data, expected_len: _ } = self; |
39 | | - data |
40 | | - } |
41 | | - |
42 | | - /// Returns the length of the buffer. |
43 | | - public fun len(self: &Buffer): u64 { |
44 | | - vector::length(&self.data) |
45 | | - } |
46 | | - |
47 | | - /// Returns `true` if the buffer is empty. |
48 | | - public fun is_empty(self: &Buffer): bool { |
49 | | - vector::is_empty(&self.data) |
50 | | - } |
| 4 | +module book::buffer; |
| 5 | + |
| 6 | +/// Error returned when the buffer is overflowed. |
| 7 | +const EBufferOverflow: u64 = 0; |
| 8 | + |
| 9 | +/// The Buffer struct represents a growable buffer. |
| 10 | +public struct Buffer { |
| 11 | + data: vector<u8>, |
| 12 | + expected_len: Option<u64> |
| 13 | +} |
| 14 | + |
| 15 | +/// Creates a new empty buffer. |
| 16 | +public fun new(): Buffer { |
| 17 | + Buffer { data: vector[], expected_len: option::none() } |
| 18 | +} |
| 19 | + |
| 20 | +/// Creates a new empty buffer with the specified capacity (in bytes). |
| 21 | +/// If the buffer is overflowed, tx aborts with `EBufferOverflow`. |
| 22 | +public fun alloc(len: u64): Buffer { |
| 23 | + Buffer { data: vector[], expected_len: option::some(len) } |
| 24 | +} |
| 25 | + |
| 26 | +/// Pushes the given data to the end of the buffer. |
| 27 | +public fun push(self: &mut Buffer, data: vector<u8>) { |
| 28 | + self.expected_len.do_ref!(|max_len| assert!(self.len() + data.length() <= *max_len, EBufferOverflow)); |
| 29 | + self.data.append(data) |
| 30 | +} |
| 31 | + |
| 32 | +/// Unwraps the buffer and returns the underlying vector. |
| 33 | +public fun unwrap(self: Buffer): vector<u8> { |
| 34 | + let Buffer { data, expected_len: _ } = self; |
| 35 | + data |
| 36 | +} |
| 37 | + |
| 38 | +/// Returns the length of the buffer. |
| 39 | +public fun len(self: &Buffer): u64 { |
| 40 | + self.data.length() |
| 41 | +} |
| 42 | + |
| 43 | +/// Returns `true` if the buffer is empty. |
| 44 | +public fun is_empty(self: &Buffer): bool { |
| 45 | + self.data.is_empty() |
51 | 46 | } |
0 commit comments