Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions library/core/src/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,26 @@ impl<T> Cell<[T]> {
}
}

impl<T, const N: usize> Cell<[T; N]> {
/// Returns a `&[Cell<T>; N]` from a `&Cell<[T; N]>`
///
/// # Examples
///
/// ```
/// #![feature(as_array_of_cells)]
/// use std::cell::Cell;
///
/// let mut array: [i32; 3] = [1, 2, 3];
/// let cell_array: &Cell<[i32; 3]> = Cell::from_mut(&mut array);
/// let array_cell: &[Cell<i32>; 3] = cell_array.as_array_of_cells();
/// ```
#[unstable(feature = "as_array_of_cells", issue = "88248")]
pub fn as_array_of_cells(&self) -> &[Cell<T>; N] {
// SAFETY: `Cell<T>` has the same memory layout as `T`.
unsafe { &*(self as *const Cell<[T; N]> as *const [Cell<T>; N]) }
}
}

/// A mutable memory location with dynamically checked borrow rules
///
/// See the [module-level documentation](self) for more.
Expand Down
9 changes: 9 additions & 0 deletions src/test/ui/rfcs/rfc-1789-as-cell/from-mut.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// run-pass

#![feature(as_array_of_cells)]

use std::cell::Cell;

fn main() {
Expand All @@ -8,4 +10,11 @@ fn main() {
let slice_cell: &[Cell<i32>] = cell_slice.as_slice_of_cells();

assert_eq!(slice_cell.len(), 3);

let mut array: [i32; 3] = [1, 2, 3];
let cell_array: &Cell<[i32; 3]> = Cell::from_mut(&mut array);
let array_cell: &[Cell<i32>; 3] = cell_array.as_array_of_cells();

array_cell[0].set(99);
assert_eq!(array, [99, 2, 3]);
}