Skip to content

Commit abf0be6

Browse files
committed
Edit and expand the chapter intro
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.
1 parent 2e08ea2 commit abf0be6

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

08_traits_and_reading_bytes.md

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,22 @@ let number_of_inputs = u32::from_le_bytes(&transaction_bytes[5..6]);
1111

1212
Notice how we're grabbing different ranges of `transaction_bytes`.
1313
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+
1521
Transactions are presented in hex format for a reason.
1622
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.
1925
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.
2127
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.
2430

2531
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.
2632

@@ -44,7 +50,7 @@ fn main() {
4450
```
4551

4652
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.
4854

4955
You might also notice the `&mut` keyword in the argument to the `read` method.
5056
This indicates that we're passing in `buffer` as a *mutable reference*.
@@ -56,7 +62,8 @@ Version: 1
5662
Bytes slice: [2]
5763
```
5864

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.
6067

6168
You may notice that the way this works is that you have to first create an array with a fixed size.
6269
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

Comments
 (0)