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

Provide a method to allocate a slice of zeroed/default values #41

Closed
ztlpn opened this issue Nov 5, 2019 · 7 comments
Closed

Provide a method to allocate a slice of zeroed/default values #41

ztlpn opened this issue Nov 5, 2019 · 7 comments

Comments

@ztlpn
Copy link

ztlpn commented Nov 5, 2019

Currently to bump-allocate any slice an existing slice is needed which is not always convenient. I guess currently the workaround is to use the alloc_layout method, but a method that doesn't require unsafe would be handy. Another handy addition would be a method that allocates and fills a slice with copies/clones of a single value.

@ztlpn
Copy link
Author

ztlpn commented Nov 5, 2019

@fitzgen I can do a PR if you think that this addition makes sense.

@fitzgen
Copy link
Owner

fitzgen commented Nov 6, 2019

Can you sketch the API / method signature you're looking for here?

Zeroing memory is not going to result in a safe API in the general case (hence std::mem::zeroed being unsafe) so if your goal is avoiding unsafe I think we might need to workshop the API design a bit.

@ztlpn
Copy link
Author

ztlpn commented Nov 6, 2019

Something like

fn alloc_slice_fill_copy<T: Copy>(&self, len: usize, value: &T) -> &mut [T];
fn alloc_slice_fill_clone<T: Clone>(&self, len: usize, value: &T) -> &mut [T];
fn alloc_slice_fill_default<T: Default>(&self, len: usize) -> &mut [T];

Basically the equivalent of vec![len; value].

Zeroing memory is not going to result in a safe API in the general case

Yeah, I don't need zeroed memory in particular, but slice with default values. It was just that I had integer types in mind when I created the issue.

@fitzgen
Copy link
Owner

fitzgen commented Nov 6, 2019

That looks great -- happy to take a PR implementing those APIs!

@fitzgen
Copy link
Owner

fitzgen commented Nov 22, 2019

I believe this is fully implemted now. If I'm mistaken, please re-open!

@fitzgen fitzgen closed this as completed Nov 22, 2019
@ztlpn
Copy link
Author

ztlpn commented Nov 22, 2019

Thanks! @fitzgen It would be great if you could cut a release with this feature!

@fitzgen
Copy link
Owner

fitzgen commented Nov 22, 2019

See #51

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

2 participants