Skip to content

Commit a02beae

Browse files
committed
Use less mut and copy directly
1 parent 3a9f23b commit a02beae

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

pineappl_capi/src/lib.rs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1932,24 +1932,24 @@ pub unsafe extern "C" fn pineappl_grid_kinematics_len(grid: *mut Grid) -> usize
19321932
/// subgrid has to be an array whose size must be as given by `pineappl_grid_kinematics_len`.
19331933
#[no_mangle]
19341934
pub unsafe extern "C" fn pineappl_grid_subgrid_shape(
1935-
grid: *mut Grid,
1935+
grid: *const Grid,
19361936
bin: usize,
19371937
order: usize,
19381938
channel: usize,
19391939
shape: *mut usize,
19401940
) {
1941-
let grid = unsafe { &mut *grid };
1941+
let grid = unsafe { &*grid };
19421942
let subgrid = &grid.subgrids()[[order, bin, channel]];
1943-
19441943
let subgrid_shape = if subgrid.is_empty() {
1944+
// avoid calling `Subgrid::shape()` for empty grids, which may panic
19451945
let subgrid_dim = grid.kinematics().len();
19461946
&vec![0; subgrid_dim]
19471947
} else {
19481948
subgrid.shape()
19491949
};
1950-
19511950
let shape = unsafe { slice::from_raw_parts_mut(shape, grid.kinematics().len()) };
1952-
shape.copy_from_slice(subgrid_shape);
1951+
1952+
shape.copy_from_slice(&subgrid_shape);
19531953
}
19541954

19551955
/// Get the subgrid for a given bin, channel, and order
@@ -1962,23 +1962,24 @@ pub unsafe extern "C" fn pineappl_grid_subgrid_shape(
19621962
/// given by `pineappl_grid_subgrid_shape`.
19631963
#[no_mangle]
19641964
pub unsafe extern "C" fn pineappl_grid_subgrid_array(
1965-
grid: *mut Grid,
1965+
grid: *const Grid,
19661966
bin: usize,
19671967
order: usize,
19681968
channel: usize,
19691969
subgrid_array: *mut f64,
19701970
) {
1971-
let grid = unsafe { &mut *grid };
1971+
let grid = unsafe { &*grid };
19721972
let subgrid = &grid.subgrids()[[order, bin, channel]];
19731973

1974-
let subgrid_array =
1975-
unsafe { slice::from_raw_parts_mut(subgrid_array, subgrid.shape().iter().product()) };
1976-
let mut flattened_subgrid_array = vec![0.0; subgrid_array.len()];
1974+
// avoid calling `Subgrid::shape()` for empty grids, which may panic
1975+
if !subgrid.is_empty() {
1976+
let shape = subgrid.shape();
1977+
let subgrid_array =
1978+
unsafe { slice::from_raw_parts_mut(subgrid_array, shape.iter().product()) };
19771979

1978-
for (index, value) in subgrid.indexed_iter() {
1979-
let ravel_index = ravel_multi_index(index.as_slice(), subgrid.shape());
1980-
flattened_subgrid_array[ravel_index] = value;
1980+
for (index, value) in subgrid.indexed_iter() {
1981+
let ravel_index = ravel_multi_index(index.as_slice(), &shape);
1982+
subgrid_array[ravel_index] = value;
1983+
}
19811984
}
1982-
1983-
subgrid_array.copy_from_slice(&flattened_subgrid_array);
19841985
}

0 commit comments

Comments
 (0)