Skip to content
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

Add FromBytes::from_bytes and AsBytes::as_bytes #1119

Open
y86-dev opened this issue Sep 25, 2024 · 6 comments
Open

Add FromBytes::from_bytes and AsBytes::as_bytes #1119

y86-dev opened this issue Sep 25, 2024 · 6 comments
Assignees

Comments

@y86-dev
Copy link
Member

y86-dev commented Sep 25, 2024

  • add FromBytes::from_bytes[_mut]: unsafe function that converts a (mutable) slice of bytes into a (mutable) reference to Self
  • add AsBytes::as_bytes[_mut]: function that converts a (mutable) reference to Self into a (mutable) slice

Please also add examples to the documentation of the functions. These additions should be two patches.


This requires submitting a proper patch to the LKML and the Rust for Linux mailing list. Please recall to test your changes (including generating the documentation if changed, running the Rust doctests if changed, etc.), to use a proper title for the commit, to sign your commit under the Developer's Certificate of Origin and to add a Suggested-by: tag and a Link: tag to this issue. Please see https://rust-for-linux.com/contributing for details.

Please take this issue only if you are new to the kernel development process and you would like to use it as a test to submit your first patch to the kernel. Please do not take it if you do not plan to make other contributions to the kernel.

@bjorn3
Copy link
Member

bjorn3 commented Sep 25, 2024

as_bytes_mut would be unsound unless there is also a FromBytes bound. If the type implementing AsBytes has disallowed bit patterns, as_bytes_mut would allow creating a value which has one such disallowed bit pattern by using as_bytes_mut and then mutating through the &mut [u8] and finally accessing the original variable. Similar with from_bytes_mut unless there is a ToBytes bound.

@y86-dev
Copy link
Member Author

y86-dev commented Sep 25, 2024

Oh yeah that is true, good catch!

@Every2
Copy link

Every2 commented Sep 26, 2024

Hi, can I take this issue?

@y86-dev
Copy link
Member Author

y86-dev commented Sep 27, 2024

sure!

@Every2
Copy link

Every2 commented Sep 27, 2024

@y86-dev Do you mind explain the bjorn3 commentary? English isn't my main language and translating is being confuse to me. When he says "bound" is something like bound check? I mean, put a limit e.g &mut [u8] has to use only 255 as bound and something greater should be &mut [u16] . Thanks in advance!

@bjorn3
Copy link
Member

bjorn3 commented Sep 27, 2024

An example of a bound would be:

trait Foo {
    fn bar(&self) where Self: Baz;
}

Here the where Self: Baz ensures that bar can only be called if Self (the type implementing the Foo trait) implements Baz too. You can add where Self: FromBytes to fn as_bytes_mut(...) -> ... and where Self: ToBytes to fn from_bytes_mut(...) -> ....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants