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 support for arbritrary arrays #55

Merged
merged 2 commits into from Mar 29, 2021
Merged

Conversation

c410-f3r
Copy link
Contributor

T: Default would remove all unsafes but it isn't currently possible for [T; N] and this bound limitation isn't desirable for stuff that don't implement Default.

If rust-lang/rust#75644 is going to be accepted, then the auxiliary functions won't be necessary in the near future.

@fitzgen
Copy link
Member

fitzgen commented Sep 14, 2020

I would prefer not to have nightly-only features in this crate.

@c410-f3r
Copy link
Contributor Author

@fitzgen You might have a better insight but I am betting on 1.49 that is going to be released on 2020-12-31

@fitzgen
Copy link
Member

fitzgen commented Sep 14, 2020

I don't have any insider insight here, but I'd prefer revisiting after it merges to stable. Happy to leave this PR open until then.

@c410-f3r
Copy link
Contributor Author

The time has come.

Copy link
Member

@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.

Looks good to me, modulo a couple nitpicks below. Fix those up and then we can merge this! Thanks @c410-f3r!

src/lib.rs Outdated
{
let mut array: mem::MaybeUninit<[T; N]> = mem::MaybeUninit::uninit();
let mut guard: ArrayGuard<T, N> = ArrayGuard {
dst: array.as_mut_ptr() as _,
Copy link
Member

@fitzgen fitzgen Mar 29, 2021

Choose a reason for hiding this comment

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

What is the as cast doing here? Converting from *mut [T; N] to *mut T? If so, can we lift that to multiple lets that show the types, like this:

let dst: *mut [T; N] = array.as_mut_ptr();
let dst: *mut T = dst as _;
// ...

fn arbitrary_take_rest(mut u: Unstructured<'a>) -> Result<[T; $n]> {
$(let $as = $ts::arbitrary(&mut u)?;)*
let last = Arbitrary::arbitrary_take_rest(u)?;
struct ArrayGuard<T, const N: usize> {
Copy link
Member

Choose a reason for hiding this comment

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

Could you add a little documentation for what role this guard plays? That will help future readers of this code.

src/lib.rs Outdated
{
let mut array: mem::MaybeUninit<[T; N]> = mem::MaybeUninit::uninit();
let mut guard: ArrayGuard<T, N> = ArrayGuard {
dst: array.as_mut_ptr() as _,
Copy link
Member

Choose a reason for hiding this comment

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

Ditto regarding the as here.

@c410-f3r c410-f3r changed the title Add const-generics feature Add support for arbritrary arrays Mar 29, 2021
@c410-f3r
Copy link
Contributor Author

Done. Thanks for the review

@fitzgen
Copy link
Member

fitzgen commented Mar 29, 2021

Thanks!

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.

None yet

2 participants