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 from_slice to VirtAddr #442

Merged
merged 1 commit into from
Oct 14, 2023
Merged

add from_slice to VirtAddr #442

merged 1 commit into from
Oct 14, 2023

Conversation

Wasabi375
Copy link
Contributor

@Wasabi375 Wasabi375 commented Oct 13, 2023

Normally it's easily possible to create a VirtAddr via a reference using the from_ptr function. However this doesn't work with slices, because they are unsized.

My first idea was to simply modify from_ptr<T> to from_ptr<T: Sized?>, however that does not work because slices are wide pointers and therefor have to be cast into a narrow pointer first.

@Freax13
Copy link
Member

Freax13 commented Oct 13, 2023

My first idea was to simply modify from_ptr<T> to from_ptr<T: Sized?>, however that does not work because slices are wide pointers and therefor have to be cast into a narrow pointer first.

Is that a problem though? Can't we just cast through a thin pointer? i.e.

    pub fn from_ptr<T>(ptr: *const T) -> Self
    where
        T: ?Sized,
    {
        Self::new(ptr as *const () as u64)
    }

@josephlr
Copy link
Contributor

Is that a problem though? Can't we just cast through a thin pointer? i.e.

I like this approach for adding support for T: ?Sized

@Wasabi375
Copy link
Contributor Author

I did not think of casting to *const (). I like that solution. I updated the PR

Copy link
Contributor

@josephlr josephlr left a comment

Choose a reason for hiding this comment

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

Looks good, could we add a unit test for using from_ptr with an unsized argument?

Copy link
Contributor

@josephlr josephlr left a comment

Choose a reason for hiding this comment

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

Thanks!!

Copy link
Member

@Freax13 Freax13 left a comment

Choose a reason for hiding this comment

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

Looks good to me, thanks!

@Freax13 Freax13 merged commit d891bdb into rust-osdev:master Oct 14, 2023
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants