-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
make creating arrays of types that are not Copy
less painful
#1109
Comments
This is actually problematic regarding idempotency: Currently |
[value; N]
sugarClone
less painful
I think you mean not |
Clone
less painfulCopy
less painful
The way I would do it is by supporting both In the case of constants, the "syntactic" expansion that you propose would be correct, as one could always write the same expression multiple times and it would result in the same value. |
Maybe we should have a new construct that is equivalent to macro_rules! init_array(
($ty:ty, $len:expr, $val:expr) => (
{
let mut array: [$ty; $len] = unsafe { std::mem::uninitialized() };
for i in array.iter_mut() {
unsafe { ::std::ptr::write(i, $val); }
}
array
}
)
);
fn main() {
let myarray = init_array!(AtomicBool, 10, AtomicBool::new(false));
} [1] https://www.reddit.com/r/rust/comments/33xhhu/how_to_create_an_array_of_structs_that_havent/cqqf9tr |
I would suggest not adding anything language-level because that usecase can be fulfilled with iterators and collecting to |
We still need to solve the panic-safeness problem, don't we? |
With integer values in generics and #197 it would be possible to define a |
Re @eddyb's suggestion of supporting
|
@benw I guess the reformulation required there would be "ADT tree of The only other kind of non- But at least that's a backwards-compatible change to make later - and this "ADT tree of |
Now that we have use ::std::mem::{uninitialized, ManuallyDrop};
use ::std::ptr;
macro_rules! yarr(
($val:expr; $len:expr)
=> {
{
// (this might panic... of consequence to nobody, anywhere)
let x = $val;
let array: [_; $len] = unsafe { uninitialized() };
let mut array = ManuallyDrop::new(array);
// (of course, one could micro-optimize this to do $n-1 clones...)
for p in &mut *array {
// clone() may panic, but if this occurs then the elements
// of array will simply be leaked without risk of dropping
// uninitialized data.
unsafe { ::std::ptr::write(p, x.clone()); }
}
ManuallyDrop::into_inner(array)
}
};
)
fn main() {
println!("{:?}", yarr!["Yo ho ho".to_string(); 3])
} |
A common need: let mut m = [vec![]; 100]; |
Closing in favor of rust-lang/rust#49147 since that covers most of the use cases noted here. |
It would be very nice if
was syntactic sugar for
This would allow things like
Currently it only works for values that implement
Copy
. For the use case above, there is currently not really any alternative to just writing[$expr, $expr, ...]
.I think this is a backwards-compatible change.
The text was updated successfully, but these errors were encountered: