Skip to content

Tricky to construct a dynamically sized ~[T] #13658

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

Closed
SiegeLord opened this issue Apr 21, 2014 · 4 comments
Closed

Tricky to construct a dynamically sized ~[T] #13658

SiegeLord opened this issue Apr 21, 2014 · 4 comments

Comments

@SiegeLord
Copy link
Contributor

With the removal of ~[T] vector building functions by #13588, it is now very difficult to construct ~[T] with a size known at runtime. Currently I opted for this replacement of std::slice::from_elem:

fn from_elem<T: Clone>(size: uint, el: T) -> ~[T]
{
    use std::iter::Repeat;
    Repeat::new(el).take(size).collect()
}

but it is unclear to me if this workaround is not merely an oversight. Is the intention to make dynamically sized ~[T] impossible to construct?

@alexcrichton
Copy link
Member

We have yet to provide a convenient conversion from Vec<T> to ~[T], but one will be provided. Your example would look like:

let foo: ~[T] = Vec::from_elem(n, e).insert_method_name_here();

Currently insert_method_name_here() is move_iter().collect().

@gereeter
Copy link
Contributor

Even assuming proper conversion from Vec<T> to ~[T], I think this brings up a valid point. slice::from_fn and slice::from_elem are not in anyway problematic with DST as they are provided a length at runtime. All the other functions removed in #13588 have to do with growing and shrinking, but from_fn and from_elem do not. It seems perfectly reasonable to reinstate them.

@thestinger
Copy link
Contributor

The ~[T] type has been removed.

@SiegeLord
Copy link
Contributor Author

It's tricky to construct a dynamically sized Box<[T]> too, so it seems this is still valid... but w/e.

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

No branches or pull requests

4 participants