Skip to content

std: sys: io: io_slice: Add UEFI types #144350

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Ayush1325
Copy link
Contributor

UEFI networking APIs do support vectored read/write. While the types for UDP4, UDP6, TCP4 and TCP6 are defined separately, they are essentially the same C struct. So we can map IoSlice and IoSliceMut to have the same binary representation.

Since all UEFI networking types for read/write are DSTs, IoSlice and IoSliceMut will need to be copied to the end of the transmit/receive structures. So having the same binary representation just allows us to do a single memcpy instead of having to loop and set the DST.

cc @nicholasbishop

@rustbot
Copy link
Collaborator

rustbot commented Jul 23, 2025

r? @thomcc

rustbot has assigned @thomcc.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jul 23, 2025
@Ayush1325 Ayush1325 force-pushed the uefi-io branch 2 times, most recently from 5ee308f to 17cd61d Compare July 23, 2025 07:33
// that `IoSlice` and `IoSliceMut` are compatible with the vectored types for all the
// networking protocols.

fn to_slice<T>(val: &T) -> &[u8] {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even though this is just test code, the function is unsafe and should be marked as such.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

#[inline]
pub fn advance(&mut self, n: usize) {
let count: u32 = n.try_into().unwrap();
self.len += count;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be subtracting, not adding?

A couple other notes: I think this should use checked arithmetic since advance is supposed to panic "when trying to advance beyond the end of the slice". Also the Unix and Windows impls also have a nice "advancing IoSlice beyond its length" panic message which would be good to include here.

Something like:

    self.len = u32::try_from(n)
        .ok()
        .and_then(|n| self.len.checked_sub(n))
        .expect("advancing IoSlice beyond its length");

(Ditto for the mut advance impl)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

];

assert_eq!(to_slice(&lhs), to_slice(&rhs));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good to add a test or two for advance/as_slice/into_slice/as_mut_slice

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added

UEFI networking APIs do support vectored read/write. While the types for
UDP4, UDP6, TCP4 and TCP6 are defined separately, they are essentially
the same C struct. So we can map IoSlice and IoSliceMut to have the same
binary representation.

Since all UEFI networking types for read/write are DSTs, `IoSlice` and
`IoSliceMut` will need to be copied to the end of the transmit/receive
structures. So having the same binary representation just allows us to
do a single memcpy instead of having to loop and set the DST.

Signed-off-by: Ayush Singh <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants