Skip to content

How to use DataOwned::MaybeUninit ? #987

@cassiersg

Description

@cassiersg

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 ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions