-
Notifications
You must be signed in to change notification settings - Fork 286
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Overhaul Pages and Bytes types #449
Merged
Changes from 8 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
a5c29bd
add Pages and Bytes to wasmi_core and remove memory_units dependency
Robbepop 3f8f7ff
adjust wasmi crate for changes in wasmi_core
Robbepop cb4bf1f
apply rustfmt
Robbepop d3afaab
Merge branch 'master' of github.com:paritytech/wasmi into rf-units
Robbepop d84e4a0
apply clippy suggestions
Robbepop 889b301
apply rustfmt
Robbepop 3cebef0
Merge branch 'master' of github.com:paritytech/wasmi into rf-units
Robbepop 4985283
Merge branch 'master' of github.com:paritytech/wasmi into rf-units
Robbepop 6d42d35
Merge branch 'master' of github.com:paritytech/wasmi into rf-units
Robbepop 9bf8bad
Merge branch 'master' of github.com:paritytech/wasmi into rf-units
Robbepop cc1589d
Merge branch 'master' of github.com:paritytech/wasmi into rf-units
Robbepop a5e343a
add more docs to some Pages methods
Robbepop a78a616
add infallible From<u16> constructor impl for Pages
Robbepop 7629821
simplify Pages::checked_add impl
Robbepop f16fe9d
apply rustfmt (?)
Robbepop d5909f9
redesign Pages and Bytes entirely (again)
Robbepop 1d14a9e
apply clippy suggestions
Robbepop File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
use core::ops::Add; | ||
|
||
/// An amount of linear memory pages. | ||
#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)] | ||
#[repr(transparent)] | ||
pub struct Pages(u32); | ||
|
||
impl Pages { | ||
/// The maximum amount of pages on the `wasm32` target. | ||
pub const fn max() -> Self { | ||
Self(65536) // 2^16 | ||
} | ||
} | ||
|
||
impl Pages { | ||
/// Creates a new amount of [`Pages`] if the amount is within bounds. | ||
pub fn new(amount: u32) -> Option<Self> { | ||
if amount > u32::from(Self::max()) { | ||
return None; | ||
} | ||
Some(Self(amount)) | ||
} | ||
|
||
/// Adds the given amount of pages to `self`. | ||
/// | ||
/// Returns `Some` if the result is within bounds and `None` otherwise. | ||
pub fn checked_add<T>(self, rhs: T) -> Option<Self> | ||
where | ||
T: Into<u32>, | ||
{ | ||
let lhs = u32::from(self); | ||
let rhs = rhs.into(); | ||
let max = u32::from(Self::max()); | ||
lhs.checked_add(rhs) | ||
.filter(move |&result| result <= max) | ||
.map(Self) | ||
} | ||
|
||
/// Returns the amount of bytes required for the amount of [`Pages`]. | ||
pub fn to_bytes(self) -> Bytes { | ||
Bytes::from(self) | ||
} | ||
} | ||
|
||
impl From<Pages> for u32 { | ||
fn from(pages: Pages) -> Self { | ||
pages.0 | ||
} | ||
} | ||
|
||
/// An amount of bytes of a linear memory. | ||
#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)] | ||
#[repr(transparent)] | ||
pub struct Bytes(u64); | ||
|
||
impl Bytes { | ||
/// The bytes per page on the `wasm32` target. | ||
pub const fn per_page() -> Self { | ||
Self(65536) // 2^16 | ||
} | ||
} | ||
|
||
impl Add<Pages> for Bytes { | ||
type Output = Self; | ||
|
||
fn add(self, pages: Pages) -> Self::Output { | ||
let lhs = u64::from(self); | ||
let rhs = u64::from(pages.to_bytes()); | ||
Self(lhs + rhs) | ||
} | ||
} | ||
|
||
impl From<u64> for Bytes { | ||
fn from(bytes: u64) -> Self { | ||
Self(bytes) | ||
} | ||
} | ||
|
||
impl From<Pages> for Bytes { | ||
fn from(pages: Pages) -> Self { | ||
Self(u64::from(u32::from(pages)) * u64::from(Self::per_page())) | ||
} | ||
} | ||
|
||
impl From<Bytes> for u64 { | ||
fn from(bytes: Bytes) -> Self { | ||
bytes.0 | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I could not find this value in spec. Could you give me a ref?