-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add MIGRATION GUIDE and CHANGELOG for this work
- Loading branch information
Showing
2 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
## Changing slice by array reference | ||
|
||
For slice to array reference changes of the form: | ||
|
||
``` | ||
fn function(value: &[u8]) { ... } | ||
``` | ||
|
||
tto: | ||
|
||
``` | ||
fn function(value: &[u8; 12]) { ... } | ||
``` | ||
|
||
In the case of the caller using array of the right size already, | ||
no changes need to be done. When the caller is using a subslice, | ||
one can use the following construction, from: | ||
|
||
``` | ||
fn caller() { | ||
let slice = &[....]; | ||
function(&slice[0..12]); | ||
} | ||
``` | ||
|
||
to: | ||
|
||
``` | ||
use core::convert::TryFrom; // not-necessary in latest rust edition | ||
fn caller() { | ||
let slice = &[....]; | ||
function(<&[u8; 12]>::try_from(&slice[0..12]).unwrap()); | ||
} | ||
Note the .unwrap() is just one way to (not) handle the error, and the caller | ||
should integrate the failing `try_from` case with the error handling | ||
conventions of the caller code. |