Skip to content

RandomEngine instances are consumed by random_normal() and random_uniform() #162

@maddiemort

Description

@maddiemort

I'm not sure if I'm fully understanding the way the Rust wrapper interacts with the C API, but it seems strange/inconvenient to me that random_normal() and random_uniform() consume instances of RandomEngine rather than taking a &RandomEngine or a &mut RandomEngine. For example:

extern crate arrayfire;
extern crate time;

use arrayfire::*;

fn foo() {
    let now = time::get_time().sec as u64;
    let random_engine = RandomEngine::new(RandomEngineType::PHILOX_4X32_10, Some(now));

    let dims = Dim4::new(&[4, 1, 1, 1]);

    let rand1 = random_normal::<f32>(dims,
        random_engine // `random_engine` is moved here
    ); 
    
    let rand2 = random_normal::<f32>(dims,
        random_engine // `random_engine` can't be used here, because it's been moved
    );
}

In that example, random_engine is moved by the first call to random_normal(), so it can't be used again in the second call. Replacing random_engine with random_engine.clone() fixes this, but it seems strange that that needs to be explicitly called. Am I misunderstanding how the Rust wrapper works, or would it make sense either for RandomEngine (because it is simply a wrapper around an FFI handle) to #[derive(Copy)] or for random_normal() and random_uniform() to be declared as follows?

pub fn random_uniform<T: HasAfEnum>(dims: Dim4, engine: &RandomEngine) -> Array { /* ... */ }
pub fn random_normal<T: HasAfEnum>(dims: Dim4, engine: &RandomEngine) -> Array { /* ... */ }
//            A shared reference rather than ownership ^^^^^^^^^^^^^

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