|
| 1 | +use std::any::Any; |
| 2 | + |
| 3 | +use array_box::ArrayBox; |
| 4 | + |
| 5 | +use value_box::{BoxerError, ReturnBoxerResult, ValueBox, ValueBoxPointer}; |
| 6 | + |
| 7 | +pub trait ArrayBoxFFI<T> |
| 8 | +where |
| 9 | + T: Default + Copy + Any, |
| 10 | +{ |
| 11 | + fn boxer_array_byte_size(count: usize) -> usize; |
| 12 | + fn boxer_array_create() -> *mut ValueBox<ArrayBox<T>>; |
| 13 | + fn boxer_array_create_with(element: T, amount: usize) -> *mut ValueBox<ArrayBox<T>>; |
| 14 | + |
| 15 | + fn boxer_array_create_from_data(_data: *mut T, amount: usize) -> *mut ValueBox<ArrayBox<T>>; |
| 16 | + |
| 17 | + fn boxer_array_drop(ptr: *mut ValueBox<ArrayBox<T>>); |
| 18 | + |
| 19 | + fn boxer_array_copy_into( |
| 20 | + _maybe_null_source_ptr: *mut ValueBox<ArrayBox<T>>, |
| 21 | + _maybe_null_destination_ptr: *mut ValueBox<ArrayBox<T>>, |
| 22 | + ); |
| 23 | + |
| 24 | + fn boxer_array_copy_into_data( |
| 25 | + _maybe_null_source_ptr: *mut ValueBox<ArrayBox<T>>, |
| 26 | + _destination_data: *mut T, |
| 27 | + length: usize, |
| 28 | + ); |
| 29 | + |
| 30 | + fn boxer_array_get_length(_maybe_null_ptr: *mut ValueBox<ArrayBox<T>>) -> usize; |
| 31 | + |
| 32 | + fn boxer_array_get_capacity(_maybe_null_ptr: *mut ValueBox<ArrayBox<T>>) -> usize; |
| 33 | + |
| 34 | + fn boxer_array_get_data(_maybe_null_ptr: *mut ValueBox<ArrayBox<T>>) -> *mut T; |
| 35 | + |
| 36 | + fn boxer_array_at_put(_maybe_null_ptr: *mut ValueBox<ArrayBox<T>>, index: usize, item: T) |
| 37 | + where |
| 38 | + T: Clone; |
| 39 | + |
| 40 | + fn boxer_array_at(_maybe_null_ptr: *mut ValueBox<ArrayBox<T>>, index: usize, default: T) -> T |
| 41 | + where |
| 42 | + T: Clone; |
| 43 | +} |
| 44 | + |
| 45 | +impl<T> ArrayBoxFFI<T> for ArrayBox<T> |
| 46 | +where |
| 47 | + T: Default + Copy + Any, |
| 48 | +{ |
| 49 | + fn boxer_array_byte_size(count: usize) -> usize { |
| 50 | + std::mem::size_of::<T>() * count |
| 51 | + } |
| 52 | + |
| 53 | + fn boxer_array_create() -> *mut ValueBox<ArrayBox<T>> { |
| 54 | + ValueBox::new(ArrayBox::<T>::default()).into_raw() |
| 55 | + } |
| 56 | + |
| 57 | + fn boxer_array_create_with(element: T, amount: usize) -> *mut ValueBox<ArrayBox<T>> { |
| 58 | + ValueBox::new(ArrayBox::<T>::from_vector(vec![element; amount])).into_raw() |
| 59 | + } |
| 60 | + |
| 61 | + fn boxer_array_create_from_data(_data: *mut T, amount: usize) -> *mut ValueBox<ArrayBox<T>> { |
| 62 | + ValueBox::new(ArrayBox::<T>::from_data(_data, amount)).into_raw() |
| 63 | + } |
| 64 | + |
| 65 | + fn boxer_array_drop(ptr: *mut ValueBox<ArrayBox<T>>) { |
| 66 | + ptr.release(); |
| 67 | + } |
| 68 | + |
| 69 | + fn boxer_array_copy_into( |
| 70 | + source_array: *mut ValueBox<ArrayBox<T>>, |
| 71 | + destination_array: *mut ValueBox<ArrayBox<T>>, |
| 72 | + ) { |
| 73 | + source_array |
| 74 | + .with_ref(|source_array| { |
| 75 | + destination_array.with_mut_ok(|destination_array| { |
| 76 | + source_array.copy_into(destination_array); |
| 77 | + }) |
| 78 | + }) |
| 79 | + .log(); |
| 80 | + } |
| 81 | + |
| 82 | + fn boxer_array_copy_into_data( |
| 83 | + source_array: *mut ValueBox<ArrayBox<T>>, |
| 84 | + destination_data: *mut T, |
| 85 | + length: usize, |
| 86 | + ) { |
| 87 | + source_array |
| 88 | + .with_ref(|source_array| { |
| 89 | + if source_array.length > length { |
| 90 | + BoxerError::AnyError( |
| 91 | + format!( |
| 92 | + "The source (len = {}) does not fit into destination (len = {})", |
| 93 | + source_array.length, length |
| 94 | + ) |
| 95 | + .into(), |
| 96 | + ) |
| 97 | + .into() |
| 98 | + } else if source_array.data.is_null() { |
| 99 | + BoxerError::AnyError("The source data must not be nil".into()).into() |
| 100 | + } else if destination_data.is_null() { |
| 101 | + BoxerError::AnyError("The destination data must not be nil".into()).into() |
| 102 | + } else { |
| 103 | + Ok(unsafe { |
| 104 | + std::ptr::copy_nonoverlapping::<T>( |
| 105 | + source_array.data, |
| 106 | + destination_data, |
| 107 | + length, |
| 108 | + ) |
| 109 | + }) |
| 110 | + } |
| 111 | + }) |
| 112 | + .log(); |
| 113 | + } |
| 114 | + |
| 115 | + fn boxer_array_get_length(array_box: *mut ValueBox<ArrayBox<T>>) -> usize { |
| 116 | + array_box.with_ref_ok(|array| array.length).or_log(0) |
| 117 | + } |
| 118 | + |
| 119 | + fn boxer_array_get_capacity(array_box: *mut ValueBox<ArrayBox<T>>) -> usize { |
| 120 | + array_box.with_ref_ok(|array| array.capacity).or_log(0) |
| 121 | + } |
| 122 | + |
| 123 | + fn boxer_array_get_data(array_box: *mut ValueBox<ArrayBox<T>>) -> *mut T { |
| 124 | + array_box |
| 125 | + .with_ref_ok(|array| array.data) |
| 126 | + .or_log(std::ptr::null_mut()) |
| 127 | + } |
| 128 | + |
| 129 | + fn boxer_array_at_put(array_box: *mut ValueBox<ArrayBox<T>>, index: usize, item: T) |
| 130 | + where |
| 131 | + T: Clone, |
| 132 | + { |
| 133 | + array_box |
| 134 | + .with_mut_ok(|array| array.at_put(index, item)) |
| 135 | + .log(); |
| 136 | + } |
| 137 | + |
| 138 | + fn boxer_array_at(array_box: *mut ValueBox<ArrayBox<T>>, index: usize, default: T) -> T |
| 139 | + where |
| 140 | + T: Clone, |
| 141 | + { |
| 142 | + array_box |
| 143 | + .with_ref_ok(|array| array.at(index)) |
| 144 | + .or_log(default) |
| 145 | + } |
| 146 | +} |
0 commit comments