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 try_ functions for slices and str #260

Merged
merged 3 commits into from
Jan 24, 2025

Conversation

c-cube
Copy link
Contributor

@c-cube c-cube commented Dec 26, 2024

It's good to be able to deal with allocation errors explicitly.

Copy link
Owner

@fitzgen fitzgen left a comment

Choose a reason for hiding this comment

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

Thanks! And sorry about the delay. This looks good, but just a few little comments below.

src/lib.rs Outdated
{
let layout = Layout::for_value(src);
let dst = self.try_alloc_layout(layout)?.cast::<T>();
let result = unsafe { slice::from_raw_parts_mut(dst.as_ptr(), src.len()) };
Copy link
Owner

Choose a reason for hiding this comment

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

I don't think this is safe because it is creating a reference of potentially uninitialized data. I think this needs to use std::ptr::copy_nonoverlapping instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you, good point. core::ptr::copy_nonoverlapping for the no_std case.

src/lib.rs Outdated Show resolved Hide resolved
Copy link
Owner

@fitzgen fitzgen left a comment

Choose a reason for hiding this comment

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

Thanks!

@fitzgen fitzgen merged commit 0713b5f into fitzgen:main Jan 24, 2025
8 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.

2 participants