TudbuT / micro_ndarray

Very small multi-dimensional-array implementation
MIT License
1 stars 0 forks source link

Initialize from `Vec` #7

Closed Tnze closed 1 year ago

Tnze commented 1 year ago

Previously, I made several requests to add more initialization methods. But now I found that I need more.

I noticed that the previous Allocator support made the code no longer elegant and I'm sorry for that.

I have a new idea that we can provide these two method for initialize the Array:

impl<T, D> Array<T, D> {
        fn from_vec(size: [usize; D], data: Vec<T>) -> Self; // ? Should we provide this? It should panic if size is not match. Or add `unsafe` and don't panic?
        fn try_from_vec(size: [usize; D], data: Vec<T>) -> Result<Self>;
}

Due to the various initialization methods included in Vec, as long as data is allowed to be initialized from Vec, we can directly utilize all the currently available methods for Vec.

I also suggest removing some of the previous APIs as soon as possible and upgrading the main version number before more users use them, to improve the maintainability of the library.

Tnze commented 1 year ago

Or impl TryFrom<Vec> for Array ?

TudbuT commented 1 year ago

I noticed that the previous Allocator support made the code no longer elegant and I'm sorry for that.

I honestly don't find it that bad. I can probably even make it smaller again, but I do agree it's a bit less elegant.


This feature actually already exists: from_flattened and into_flattened provide exactly this functionality.

However, I just noticed with great horror that the from_flattened method is unsafe and not marked as such because it violates a guarantee that the vec has the right length. I will fix this ASAP.

TudbuT commented 1 year ago

BTW, do you have an account on Discord or something similar, for example? I think talking there would be more efficient than here due to me not checking this very often.

TudbuT commented 1 year ago

Anyways: the unsafety is now fixed in 0.6.0.