Skip to content

Commit

Permalink
Perfom casts to inherited types in Rust
Browse files Browse the repository at this point in the history
  • Loading branch information
cschwan committed Sep 20, 2022
1 parent a38ee57 commit e30ade2
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 56 deletions.
2 changes: 1 addition & 1 deletion pineappl_cli/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ fn convert_fastnlo(
);
let grid = fastnlo::convert_fastnlo_table(&file, alpha, dis_pid)?;
let results = ffi::GetCrossSection(
ffi::downcast_lhapdf_to_reader_mut(file.as_mut().unwrap()),
ffi::static_cast_lhapdf_to_reader_mut(file.as_mut().unwrap()),
false,
);

Expand Down
17 changes: 11 additions & 6 deletions pineappl_cli/src/import/fastnlo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@ use pineappl::sparse_array3::SparseArray3;
use pineappl::subgrid::{Mu2, SubgridParams};
use pineappl_fastnlo::ffi::{
self, fastNLOCoeffAddBase, fastNLOCoeffAddFix, fastNLOCoeffAddFlex, fastNLOLHAPDF,
fastNLOPDFLinearCombinations, EScaleFunctionalForm,
fastNLOPDFLinearCombinations, fastNLOReader, fastNLOTable, EScaleFunctionalForm,
};
use std::convert::{TryFrom, TryInto};
use std::f64::consts::TAU;

// needed to cast between different types that are the same in C++ through inheritance
unsafe fn static_cast<T, U>(x: &U) -> &T {
&*((x as *const U) as *const T)
}

fn pid_to_pdg_id(pid: i32) -> i32 {
match pid {
-6..=-1 | 1..=6 => pid,
Expand Down Expand Up @@ -95,7 +100,7 @@ fn convert_coeff_add_fix(
alpha: u32,
dis_pid: i32,
) -> Grid {
let table_as_add_base = ffi::downcast_coeff_add_fix_to_base(table);
let table_as_add_base: &fastNLOCoeffAddBase = unsafe { static_cast(table) };

let mut grid = Grid::new(
create_lumi(table_as_add_base, comb, dis_pid),
Expand Down Expand Up @@ -214,7 +219,7 @@ fn convert_coeff_add_flex(
ipub_units: i32,
dis_pid: i32,
) -> Grid {
let table_as_add_base = ffi::downcast_coeff_add_flex_to_base(table);
let table_as_add_base: &fastNLOCoeffAddBase = unsafe { static_cast(table) };

let mut grid = Grid::new(
create_lumi(table_as_add_base, comb, dis_pid),
Expand Down Expand Up @@ -406,8 +411,8 @@ fn convert_coeff_add_flex(
}

pub fn convert_fastnlo_table(file: &fastNLOLHAPDF, alpha: u32, dis_pid: i32) -> Result<Grid> {
let file_as_reader = ffi::downcast_lhapdf_to_reader(file);
let file_as_table = ffi::downcast_lhapdf_to_table(file);
let file_as_reader: &fastNLOReader = unsafe { static_cast(file) };
let file_as_table: &fastNLOTable = unsafe { static_cast(file) };

let bins: usize = file_as_table.GetNObsBin().try_into().unwrap();
let mut grids = Vec::new();
Expand All @@ -426,7 +431,7 @@ pub fn convert_fastnlo_table(file: &fastNLOLHAPDF, alpha: u32, dis_pid: i32) ->
break;
}

let linear_combinations = ffi::downcast_reader_to_pdf_linear_combinations(file_as_reader);
let linear_combinations: &fastNLOPDFLinearCombinations = unsafe { static_cast(file) };

let converted = unsafe { ffi::dynamic_cast_coeff_add_fix(coeff_base) };

Expand Down
28 changes: 1 addition & 27 deletions pineappl_fastnlo/src/fastnlo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,27 +158,7 @@ std::size_t GetNx(fastNLOCoeffAddFlex const& coeffs, std::size_t obs)
return coeffs.GetSigmaTildes().at(0)->at(obs).size();
}

fastNLOCoeffAddBase const& downcast_coeff_add_fix_to_base(fastNLOCoeffAddFix const& coeffs)
{
return coeffs;
}

fastNLOCoeffAddBase const& downcast_coeff_add_flex_to_base(fastNLOCoeffAddFlex const& coeffs)
{
return coeffs;
}

fastNLOReader const& downcast_lhapdf_to_reader(fastNLOLHAPDF const& lhapdf)
{
return lhapdf;
}

fastNLOReader& downcast_lhapdf_to_reader_mut(fastNLOLHAPDF& lhapdf)
{
return lhapdf;
}

fastNLOTable const& downcast_lhapdf_to_table(fastNLOLHAPDF const& lhapdf)
fastNLOReader& static_cast_lhapdf_to_reader_mut(fastNLOLHAPDF& lhapdf)
{
return lhapdf;
}
Expand All @@ -203,12 +183,6 @@ fastNLOCoeffMult const* dynamic_cast_coeff_mult(fastNLOCoeffBase const* coeffs)
return dynamic_cast <fastNLOCoeffMult const*> (coeffs);
}

fastNLOPDFLinearCombinations const& downcast_reader_to_pdf_linear_combinations(
fastNLOReader const& reader
) {
return reader;
}

pair_double_double GetObsBinDimBounds(
fastNLOTable const& table,
unsigned int iObs,
Expand Down
14 changes: 1 addition & 13 deletions pineappl_fastnlo/src/fastnlo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,7 @@ double GetSigmaTilde(

std::size_t GetNx(fastNLOCoeffAddFlex const& coeffs, std::size_t);

fastNLOCoeffAddBase const& downcast_coeff_add_fix_to_base(fastNLOCoeffAddFix const& coeffs);

fastNLOCoeffAddBase const& downcast_coeff_add_flex_to_base(fastNLOCoeffAddFlex const& coeffs);

fastNLOReader const& downcast_lhapdf_to_reader(fastNLOLHAPDF const& lhapdf);

fastNLOReader& downcast_lhapdf_to_reader_mut(fastNLOLHAPDF& lhapdf);

fastNLOTable const& downcast_lhapdf_to_table(fastNLOLHAPDF const& lhapdf);
fastNLOReader& static_cast_lhapdf_to_reader_mut(fastNLOLHAPDF& lhapdf);

fastNLOCoeffAddFix const* dynamic_cast_coeff_add_fix(fastNLOCoeffBase const* coeffs);

Expand All @@ -80,10 +72,6 @@ fastNLOCoeffData const* dynamic_cast_coeff_data(fastNLOCoeffBase const* coeffs);

fastNLOCoeffMult const* dynamic_cast_coeff_mult(fastNLOCoeffBase const* coeffs);

fastNLOPDFLinearCombinations const& downcast_reader_to_pdf_linear_combinations(
fastNLOReader const& reader
);

pair_double_double GetObsBinDimBounds(
fastNLOTable const& table,
unsigned int iObs,
Expand Down
11 changes: 2 additions & 9 deletions pineappl_fastnlo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,15 +208,6 @@ pub mod ffi {
_: i32,
) -> f64;

fn downcast_coeff_add_fix_to_base(_: &fastNLOCoeffAddFix) -> &fastNLOCoeffAddBase;
fn downcast_coeff_add_flex_to_base(_: &fastNLOCoeffAddFlex) -> &fastNLOCoeffAddBase;
fn downcast_lhapdf_to_reader(_: &fastNLOLHAPDF) -> &fastNLOReader;
fn downcast_lhapdf_to_reader_mut(_: Pin<&mut fastNLOLHAPDF>) -> Pin<&mut fastNLOReader>;
fn downcast_lhapdf_to_table(_: &fastNLOLHAPDF) -> &fastNLOTable;
fn downcast_reader_to_pdf_linear_combinations(
_: &fastNLOReader,
) -> &fastNLOPDFLinearCombinations;

unsafe fn dynamic_cast_coeff_add_fix(
_: *const fastNLOCoeffBase,
) -> *const fastNLOCoeffAddFix;
Expand All @@ -226,6 +217,8 @@ pub mod ffi {
unsafe fn dynamic_cast_coeff_data(_: *const fastNLOCoeffBase) -> *const fastNLOCoeffData;
unsafe fn dynamic_cast_coeff_mult(_: *const fastNLOCoeffBase) -> *const fastNLOCoeffMult;

fn static_cast_lhapdf_to_reader_mut(_: Pin<&mut fastNLOLHAPDF>) -> Pin<&mut fastNLOReader>;

fn make_fastnlo_lhapdf_with_name_file_set(
name: &str,
lhapdf: &str,
Expand Down

0 comments on commit e30ade2

Please sign in to comment.