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
I took advantage of an error by 1 mistake in the original code that opens the
chpater to demonstrate how easy it is to make mistakes when manually managing
indices like so.
Copy file name to clipboardExpand all lines: 08_traits_and_reading_bytes.md
+15-8Lines changed: 15 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -11,16 +11,22 @@ let number_of_inputs = u32::from_le_bytes(&transaction_bytes[5..6]);
11
11
12
12
Notice how we're grabbing different ranges of `transaction_bytes`.
13
13
We have to repeatedly reference `transaction_bytes` and we have to keep track of the start and end indexes for each component.
14
-
This is not ideal.
14
+
This is not ideal because we can easily make mistakes.
15
+
16
+
*Note: there's an indexing mistake in the code above, can you see what it is?*
17
+
<!--
18
+
Since vec[0..4] notation is not inclusive to the end of the index, we are skipping one byte: the element at index 4.
19
+
-->
20
+
15
21
Transactions are presented in hex format for a reason.
16
22
They are designed to be serialized as byte streams that can be transmitted over the network and read one byte at a time in order.
17
-
18
-
One way to read a byte stream is to leverage Rust's standard library's [`Read`](https://doc.rust-lang.org/std/io/trait.Read.html) trait.
23
+
A better solution would be to automatically manage the indices and have a function with which we can just ask for the next bytes we require.
24
+
Rust's standard library's [`Read`](https://doc.rust-lang.org/std/io/trait.Read.html) trait allows for exactly this.
19
25
The slice data type in Rust implements the `Read` trait.
20
-
What does this mean? Well, as we will see, it gives us a method, `read`, which will read some bytes from the slice and then store that data into a array.
26
+
What does this mean? It gives us a method, `read`, which will read some bytes from the slice and return that data in an array.
21
27
When we call `read` again, it will start from where it left off.
22
-
In other words, it keeps track of where we are in the stream and modifies the pointer as it reads.
23
-
This means we don't need to keep track of any indexes.
28
+
In other words, the `read` trait includes the machinery to keep track of the current position we are reading in the stream and to manage the pointer as it proceed.
29
+
This means we don't need to keep track of any indexes ourselves.
24
30
25
31
Let's walk through how this works at a high level with a quick example and then dive deeper into what traits are and how they work.
26
32
@@ -44,7 +50,7 @@ fn main() {
44
50
```
45
51
46
52
The `mut` keyword before `bytes_slice` tells Rust the variable is mutable.
47
-
If we don't provide that keyword in a variable declaration, then the compiler will complain that we're attempting to change an immutable variable, which is not allowed.
53
+
If we don't provide that keyword in a variable declaration, then the compiler will complain that we're attempting to change the value of an immutable variable, which is not allowed.
48
54
49
55
You might also notice the `&mut` keyword in the argument to the `read` method.
50
56
This indicates that we're passing in `buffer` as a *mutable reference*.
@@ -56,7 +62,8 @@ Version: 1
56
62
Bytes slice: [2]
57
63
```
58
64
59
-
And this is what we'd expect. The Version is `1`. And the `bytes_slice` variable has been updated and no longer contains the first 4 bytes.
65
+
And this is what we'd expect.
66
+
The Version is `1` and the `bytes_slice` variable has been updated and no longer contains the first 4 bytes.
60
67
61
68
You may notice that the way this works is that you have to first create an array with a fixed size.
62
69
Calling `read` will then extract the number of bytes equal to the size of the array, store that into a buffer and then update our slice.
0 commit comments