-
Notifications
You must be signed in to change notification settings - Fork 822
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1337: feat(interface-types) Better handling of i32 to usize casts r=MarkMcCaskey a=Hywan Fix #1334 This PR introduces a new `NegativeValue` error. This PR fixes some `usize` and `u32` types for indexes (small changes). Finally, this PR casts `i32` to `usize` by using the `TryFrom` implementation. That way: ```rust let pointer = to_native::<i32>(&inputs[0], instruction)? as usize; ``` becomes: ```rust let pointer: usize = to_native::<i32>(&inputs[0], instruction)?.try_into().map_err(|_| { InstructionError::new( instruction, InstructionErrorKind::NegativeValue { subject: "pointer" }, ) })?; ``` It's more verbose, but it handles the casting correctly. Note: Maybe we should implement `From<TryFromIntError>` for `InstructionErrorKind`? What do you think? Edit: With `From<TryFromIntError>`, it looks like this: ```rust let pointer: usize = to_native::<i32>(&inputs[0], instruction)? // convert from WIT to native `i32` .try_into() // convert to `usize` .map_err(|e| (e, "pointer").into()) // convert the `TryFromIntError` to `InstructionErrorKind::NegativeValue` .map_err(|k| InstructionError::new(instruction, k))?; // convert to an `InstructionError` ``` It is indeed simpler. Keeping things like this. Co-authored-by: Ivan Enderlin <ivan.enderlin@hoa-project.net>
- Loading branch information
Showing
7 changed files
with
86 additions
and
16 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
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
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
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