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 · 8 comments
Open

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

y86-dev opened this issue Sep 25, 2024 · 8 comments
Assignees
Labels
easy Expected to be an easy issue to resolve. good first issue Good for newcomers • lib Related to the `rust/` library.

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(...) -> ....

@Every2
Copy link

Every2 commented Oct 1, 2024

Hi, I again! Since we use ToBytes with where clause, maybe we can just return Self instead &Self and use type::from_ne_bytes and guarantee that is safe independent of endianness platform?

intel-lab-lkp pushed a commit to intel-lab-lkp/linux that referenced this issue Oct 9, 2024
Add implementation and documentation for FromBytes trait.

Add new feature block in order to allow using ToBytes
and bound to from_bytes_mut function.

Link: Rust-for-Linux#1119
Signed-off-by: Christian dos Santos de Lima <[email protected]>
@ojeda ojeda added good first issue Good for newcomers easy Expected to be an easy issue to resolve. labels Oct 9, 2024
intel-lab-lkp pushed a commit to intel-lab-lkp/linux that referenced this issue Oct 12, 2024
Add implementation and documentation for FromBytes trait.

Add new feature block in order to allow using ToBytes
and bound to from_bytes_mut function. I'm adding this feature
because is possible create a value with disallowed bit pattern
and as_byte_mut could create such value by mutating the array and
acessing the original value. So adding ToBytes this can be avoided.

Link: Rust-for-Linux#1119
Signed-off-by: Christian dos Santos de Lima <[email protected]>
@Every2
Copy link

Every2 commented Oct 24, 2024

Hi, still working on it! I needed a break because my uni. Getting back to work now.

@ojeda ojeda added the • lib Related to the `rust/` library. label Nov 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
easy Expected to be an easy issue to resolve. good first issue Good for newcomers • lib Related to the `rust/` library.
Development

No branches or pull requests

4 participants