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

Easier splitting for types containing slice DSTs #2001

Closed
valaphee opened this issue Nov 1, 2024 · 3 comments
Closed

Easier splitting for types containing slice DSTs #2001

valaphee opened this issue Nov 1, 2024 · 3 comments
Labels
customer-request Documents customer requests.

Comments

@valaphee
Copy link

valaphee commented Nov 1, 2024

While working on file system drivers for a custom operating system, I'm using buffers, which basically consist of multiple entries. Each entry contains a slice DST, but it's not possible to store them as slice itself, which is why I need to split them up.

One entry might look like:

#[repr(C)]
#[derive(Debug, FromBytes, Immutable, KnownLayout)]
pub struct Entry {
    pub index: u64,
    pub name_length: u8,
    pub name: [u8],
}

where the first entry's name has the full length of the buffer.

And are split up by

let buffer_align = align_of_val(&mut buffer);
let Ok(next_buffer) = Entry::try_mut_from_bytes(
    &mut buffer.name[(buffer.name_length as usize)..].align(buffer_align),
) else {
    break;
};
buffer = next_buffer;

but requires some boilerplate. (and also a align method, which aligns the pointer)

The compiler will probably figure out the alignment at compile-time and optimize out the alignment check. But it's better to use the already known layout information.

@valaphee valaphee added the customer-request Documents customer requests. label Nov 1, 2024
@valaphee valaphee changed the title Support splitting up types containing slice DSTs Easier splitting for types containing slice DSTs Nov 1, 2024
@joshlf
Copy link
Member

joshlf commented Nov 4, 2024

For the length, we already have this tracked in #1289. Can you confirm that that issue would support your use case?

Also, can you clarify what .align does here? I'm not sure what method that refers to.

    &mut buffer.name[(buffer.name_length as usize)..].align(buffer_align),

@valaphee
Copy link
Author

valaphee commented Nov 5, 2024

In my example, Entry has to be aligned. I'm getting the end of the name, and align it to the next 8-byte, to read the next Entry. (The align method just takes the ptr, and aligns it, and corrects the length)

Had to use a custom method, as align_to doesn't work on slice DSTs.

#1289 would be helpful.

@joshlf
Copy link
Member

joshlf commented Nov 5, 2024

Okay, in that case I'm going to close this as a duplicate of #1289. Feel free to continue the conversation if you have more questions!

@joshlf joshlf closed this as completed Nov 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
customer-request Documents customer requests.
Projects
None yet
Development

No branches or pull requests

2 participants