Skip to content

How to use DataOwned::MaybeUninit ? #987

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
cassiersg opened this issue Apr 26, 2021 · 1 comment · Fixed by #1001
Closed

How to use DataOwned::MaybeUninit ? #987

cassiersg opened this issue Apr 26, 2021 · 1 comment · Fixed by #1001

Comments

@cassiersg
Copy link
Contributor

cassiersg commented Apr 26, 2021

I am trying to migrate ndarray-linalg away from the use of ArrayBase::uninitialized to ArrayBase::uninit. I do not see how to do the conversion in the following case (simplified from ndarray_linalg::convert::clone_with_layout:

fn foo<A, Si, So>(a: &ArrayBase<Si, Ix2>) -> ArrayBase<So, Ix2>
where
    A: Copy,
    Si: Data<Elem = A>,
    So: DataOwned<Elem = A> + DataMut,
{
    let layout: (usize, usize) = (1, 1); // simplified from the original
    let mut b = ArrayBase::uninitialized(layout);
    b.assign(a);
    b
}

My best attempt is:

fn foo<A, Si, So>(l: MatrixLayout, a: &ArrayBase<Si, Ix2>) -> ArrayBase<So, Ix2>
where
    A: Copy,
    Si: Data<Elem = A>,
    So: DataOwned<Elem = A> + DataMut,
{
    let layout: (usize, usize) = (1, 1); // simplified from the original
    let mut b: ArrayBase<<So as DataOwned>::MaybeUninit, Ix2> =  ArrayBase::uninit(layout);
    a.assign_to(&mut);
    unsafe { b.assume_init() }
}

which does not work because the storage for b does not impl DataMut.

Is the use of DataOwned::MaybeUninit::uninit the way to go ? Then how can I initialize it, since it doesn't implement DataMut ?

@bluss
Copy link
Member

bluss commented Apr 26, 2021

Threre is an internal way to do that (see build_uninit in the source) - but it's not quite ready for public consumption - needs to use an ArrayViewMut instead. At the moment there is no clean (it's possible with extra bounds) way to do this generically for all owned arrays.

In other words, it should currently be good enough for using specific owned arrays directly but maybe the build_uninit method needs exposing for generic methods.

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

Successfully merging a pull request may close this issue.

2 participants