Skip to content

Commit

Permalink
refactor: use #![feature(const_maybe_uninit_array_assume_init)]
Browse files Browse the repository at this point in the history
This feature, which covers the `const fn` version of `MaybeUninit::
array_assume_init`, was added by [rust-lang/rust#96099][1].

[1]: rust-lang/rust#96099
  • Loading branch information
yvt committed May 23, 2022
1 parent 6e8e26f commit 2addec3
Show file tree
Hide file tree
Showing 8 changed files with 12 additions and 49 deletions.
22 changes: 0 additions & 22 deletions doc/toolchain_limitations.md
Original file line number Diff line number Diff line change
Expand Up @@ -366,28 +366,6 @@ const _: () = { b"".iter(); };
```


### `[tag:const_array_assume_init]` `MaybeUninit::array_assume_init` is not `const fn`

```rust
#![feature(maybe_uninit_array_assume_init)]
use core::mem::MaybeUninit;
assert!(matches!(
unsafe { MaybeUninit::array_assume_init([MaybeUninit::new(42)]) },
[42]
));
```

```rust,compile_fail,E0015
#![feature(maybe_uninit_array_assume_init)]
use core::mem::MaybeUninit;
const _: () = assert!(matches!(
// error[E0015]: cannot call non-const fn `MaybeUninit::<i32>::
// array_assume_init::<1_usize>` in constants
unsafe { MaybeUninit::array_assume_init([MaybeUninit::new(42)]) },
[42]
));
```

### `[tag:const_uninit_array]` `MaybeUninit::uninit_array` is unstable

```rust,compile_fail,E0658
Expand Down
2 changes: 2 additions & 0 deletions src/r3_core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#![feature(const_maybe_uninit_array_assume_init)]
#![feature(const_fn_floating_point_arithmetic)]
#![feature(const_nonnull_slice_from_raw_parts)]
#![feature(const_maybe_uninit_uninit_array)]
#![feature(const_maybe_uninit_assume_init)]
#![feature(maybe_uninit_array_assume_init)]
#![feature(const_maybe_uninit_as_mut_ptr)]
#![feature(nonnull_slice_from_raw_parts)]
#![feature(arbitrary_enum_discriminant)]
Expand Down
17 changes: 2 additions & 15 deletions src/r3_core/src/utils/for_times.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,21 +232,8 @@ macro_rules! const_array_from_fn {
))
}

// `MaybeUninit::array_assume_init` is not `const fn` yet
// [ref:const_array_assume_init]
const unsafe fn __assume_init<
$($iter_gparam $($iter_gparam_bounds)*,)*
const LEN: usize
>(array: [MaybeUninit<$ty>; LEN]) -> [$ty; LEN] {
// Safety: This is equivalent to `transmute_copy(&array)`. The
// memory layout of `[MaybeUninit<T>; $len]` is identical to `[T; $len]`.
// We initialized all elements in `array[0..$len]`, so it's safe to
// reinterpret that range as `[T; $len]`.
unsafe { *(array.as_ptr() as *const _ as *const [$ty; LEN]) }
}

// Safety: See the body of `__assume_init`.
unsafe { __assume_init::<$($ctx_t,)* {$len_value}>(array) }
// Safety: All elements of `array` are initialized
unsafe { MaybeUninit::array_assume_init(array) }
}};
}

Expand Down
8 changes: 2 additions & 6 deletions src/r3_core/src/utils/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,8 @@ impl<T: Init, const LEN: usize> Init for [T; LEN] {
i += 1;
}

// `MaybeUninit::array_assume_init` is not `const fn` yet
// [ref:const_array_assume_init]
// Safety: The memory layout of `[MaybeUninit<T>; LEN]` is
// identical to `[T; LEN]`. We initialized all elements, so it's
// safe to reinterpret that range as `[T; LEN]`.
unsafe { super::mem::transmute(array) }
// Safety: `array`'s elements are fully initialized
unsafe { mem::MaybeUninit::array_assume_init(array) }
};
}

Expand Down
4 changes: 1 addition & 3 deletions src/r3_core/src/utils/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ mod tests {
MaybeUninit::new(2),
MaybeUninit::new(3),
];
// `MaybeUninit::array_assume_init` is not `const fn`
// [ref:const_array_assume_init]
unsafe { transmute(array) }
unsafe { MaybeUninit::array_assume_init(array) }
};
assert_eq!(ARRAY1, [1, 2, 3]);
}
Expand Down
2 changes: 2 additions & 0 deletions src/r3_kernel/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#![feature(const_maybe_uninit_array_assume_init)]
#![feature(const_maybe_uninit_uninit_array)]
#![feature(const_maybe_uninit_assume_init)]
#![feature(maybe_uninit_array_assume_init)]
#![feature(const_slice_from_raw_parts)]
#![feature(maybe_uninit_uninit_array)]
#![feature(const_precise_live_drops)]
Expand Down
4 changes: 1 addition & 3 deletions src/r3_test_suite/src/kernel_benchmarks/timer_start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ impl<System: SupportedSystem> AppInner<System> {
i += 1;
}

// `MaybeUninit::array_assume_init` is not `const fn` yet
// [ref:const_array_assume_init]
unsafe { core::mem::transmute_copy(&timers) }
unsafe { MaybeUninit::array_assume_init(timers) }
};

Self { timers }
Expand Down
2 changes: 2 additions & 0 deletions src/r3_test_suite/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#![feature(const_maybe_uninit_array_assume_init)]
#![feature(const_fn_floating_point_arithmetic)]
#![feature(maybe_uninit_array_assume_init)]
#![feature(cfg_target_has_atomic)]
#![feature(const_transmute_copy)]
#![feature(const_refs_to_cell)]
Expand Down

0 comments on commit 2addec3

Please sign in to comment.