You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
`byte_span` is a lightweight view class for efficient handling of byte data. Based on the concept of `std::span`, it provides specialized functionality for byte data manipulation.
4
+
5
+
## Features
6
+
7
+
- Non-owning view type
8
+
- Unified handling of byte-like types (`std::byte`, `char`, `unsigned char`)
9
+
- View trivially copyable data types as byte sequences
10
+
- Type-safe design using C++20 concepts
11
+
- Zero-overhead abstraction
12
+
13
+
## Basic Usage
14
+
15
+
### 1. Creating from Byte Arrays
16
+
17
+
The most basic usage is creating a `byte_span` from a byte array:
18
+
19
+
```cpp
20
+
std::byte data[4] = {
21
+
std::byte{0x00}, std::byte{0x01},
22
+
std::byte{0x02}, std::byte{0x03}
23
+
};
24
+
byte_span view{data}; // Deduced as byte_span<std::byte, 4>
25
+
```
26
+
27
+
### 2. Creating from std::array
28
+
29
+
Easy creation from `std::array`:
30
+
31
+
```cpp
32
+
std::array<std::byte, 4> arr = {
33
+
std::byte{0x00}, std::byte{0x01},
34
+
std::byte{0x02}, std::byte{0x03}
35
+
};
36
+
byte_span view{arr}; // Deduced as byte_span<std::byte, 4>
37
+
```
38
+
39
+
### 3. Creating from std::vector
40
+
41
+
You can create views from any contiguous container, including `std::vector`:
42
+
43
+
```cpp
44
+
// Integer data
45
+
std::vector<int> numbers = {1, 2, 3, 4};
46
+
byte_span view{numbers}; // View integers as bytes
47
+
48
+
// Access underlying bytes
49
+
auto first_byte = view[0];
50
+
auto size_in_bytes = view.size(); // size = sizeof(int) * numbers.size()
51
+
```
52
+
53
+
### 4. Dynamic Size Views
54
+
55
+
For runtime-sized views, use `dynamic_extent`:
56
+
57
+
```cpp
58
+
std::vector<std::byte> vec(100);
59
+
byte_span view{vec}; // Deduced as byte_span<std::byte, dynamic_extent>
60
+
```
61
+
62
+
## Advanced Usage
63
+
64
+
### 1. Using byte_view and cbyte_view
65
+
66
+
The library provides two convenient type aliases:
67
+
-`byte_view`: Mutable byte span (alias for `byte_span<std::byte>`)
68
+
-`cbyte_view`: Immutable byte span (alias for `byte_span<const std::byte>`)
0 commit comments