jlrs 0.23.0

jlrs provides bindings to the Julia C API that enable Julia code to be called from Rust and more.
Documentation
mod util;

#[cfg(test)]
#[cfg(all(feature = "local-rt", feature = "jlrs-ndarray"))]
mod tests {
    use jlrs::{
        convert::ndarray::{NdArrayView, NdArrayViewMut},
        prelude::*,
    };

    use super::util::JULIA;

    fn bits_array_view() {
        JULIA.with(|handle| {
            handle.borrow_mut().with_stack(|mut stack| {
                stack.scope(|mut frame| {
                    let mut data = vec![1usize, 2, 3, 4, 5, 6];
                    let slice = &mut data.as_mut_slice();
                    let borrowed = unsafe {
                        TypedArray::<usize>::from_slice_unchecked(&mut frame, slice, [3, 2])
                    };

                    let data = unsafe { borrowed.bits_data() };
                    let x = data[[2, 1]];

                    let array = data.array_view();
                    assert_eq!(array[[2, 1]], x);
                })
            });
        });
    }

    fn bits_array_view_mut() {
        JULIA.with(|handle| {
            handle.borrow_mut().with_stack(|mut stack| {
                stack.scope(|mut frame| unsafe {
                    let mut data = vec![1usize, 2, 3, 4, 5, 6];
                    let slice = &mut data.as_mut_slice();
                    let mut borrowed =
                        TypedArray::<usize>::from_slice_unchecked(&mut frame, slice, [3, 2]);

                    let mut inline = borrowed.bits_data_mut();
                    let x = inline[[2, 1]];

                    inline[[2, 1]] = x + 1;

                    let mut array = inline.array_view_mut();
                    assert_eq!(array[[2, 1]], x + 1);
                    array[[2, 1]] -= 1;

                    std::mem::drop(inline);
                    let inline = borrowed.bits_data_mut();
                    assert_eq!(inline[[2, 1]], x);
                })
            });
        });
    }

    fn inline_array_view() {
        JULIA.with(|handle| {
            handle.borrow_mut().with_stack(|mut stack| {
                stack.scope(|mut frame| {
                    let mut data = vec![1usize, 2, 3, 4, 5, 6];
                    let slice = &mut data.as_mut_slice();
                    let borrowed = unsafe {
                        TypedArray::<usize>::from_slice_unchecked(&mut frame, slice, [3, 2])
                    };

                    let data = unsafe { borrowed.inline_data() };
                    let x = data[[2, 1]];

                    let array = data.array_view();
                    assert_eq!(array[[2, 1]], x);
                })
            });
        });
    }

    fn copied_array_view() {
        JULIA.with(|handle| {
            handle.borrow_mut().with_stack(|mut stack| {
                stack.scope(|mut frame| {
                    let mut data = vec![1usize, 2, 3, 4, 5, 6];
                    let slice = &mut data.as_mut_slice();
                    let borrowed = unsafe {
                        TypedArray::<usize>::from_slice_unchecked(&mut frame, slice, [3, 2])
                    };
                    let copied = unsafe { borrowed.bits_data().to_copied_array() };

                    let x = copied[[2, 1]];

                    let array = copied.array_view();
                    assert_eq!(array[[2, 1]], x);
                })
            });
        });
    }

    fn copied_array_view_mut() {
        JULIA.with(|handle| {
            handle.borrow_mut().with_stack(|mut stack| {
                stack.scope(|mut frame| unsafe {
                    let mut data = vec![1usize, 2, 3, 4, 5, 6];
                    let slice = &mut data.as_mut_slice();
                    let borrowed =
                        TypedArray::<usize>::from_slice_unchecked(&mut frame, slice, [3, 2]);
                    let mut copied = borrowed.bits_data().to_copied_array();
                    let x = copied[[2, 1]];

                    copied[[2, 1]] = x + 1;

                    let mut array = copied.array_view_mut();
                    assert_eq!(array[[2, 1]], x + 1);
                    array[[2, 1]] -= 1;

                    let inline = borrowed.bits_data();
                    assert_eq!(inline[[2, 1]], x);
                })
            });
        });
    }

    #[test]
    fn ndarray_tests() {
        bits_array_view();
        bits_array_view_mut();
        inline_array_view();
        copied_array_view();
        copied_array_view_mut();
    }
}