From e30ade21a091411f5ade88d1f5d405e4e6db0bab Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Tue, 20 Sep 2022 11:07:39 +0200 Subject: [PATCH] Perfom casts to inherited types in Rust --- pineappl_cli/src/import.rs | 2 +- pineappl_cli/src/import/fastnlo.rs | 17 +++++++++++------ pineappl_fastnlo/src/fastnlo.cpp | 28 +--------------------------- pineappl_fastnlo/src/fastnlo.hpp | 14 +------------- pineappl_fastnlo/src/lib.rs | 11 ++--------- 5 files changed, 16 insertions(+), 56 deletions(-) diff --git a/pineappl_cli/src/import.rs b/pineappl_cli/src/import.rs index ee34a922..aecc522d 100644 --- a/pineappl_cli/src/import.rs +++ b/pineappl_cli/src/import.rs @@ -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, ); diff --git a/pineappl_cli/src/import/fastnlo.rs b/pineappl_cli/src/import/fastnlo.rs index 4ba34e47..418a9425 100644 --- a/pineappl_cli/src/import/fastnlo.rs +++ b/pineappl_cli/src/import/fastnlo.rs @@ -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(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, @@ -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), @@ -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), @@ -406,8 +411,8 @@ fn convert_coeff_add_flex( } pub fn convert_fastnlo_table(file: &fastNLOLHAPDF, alpha: u32, dis_pid: i32) -> Result { - 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(); @@ -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) }; diff --git a/pineappl_fastnlo/src/fastnlo.cpp b/pineappl_fastnlo/src/fastnlo.cpp index 9decf86b..81868fcc 100644 --- a/pineappl_fastnlo/src/fastnlo.cpp +++ b/pineappl_fastnlo/src/fastnlo.cpp @@ -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; } @@ -203,12 +183,6 @@ fastNLOCoeffMult const* dynamic_cast_coeff_mult(fastNLOCoeffBase const* coeffs) return dynamic_cast (coeffs); } -fastNLOPDFLinearCombinations const& downcast_reader_to_pdf_linear_combinations( - fastNLOReader const& reader -) { - return reader; -} - pair_double_double GetObsBinDimBounds( fastNLOTable const& table, unsigned int iObs, diff --git a/pineappl_fastnlo/src/fastnlo.hpp b/pineappl_fastnlo/src/fastnlo.hpp index a25f974d..f4ecabee 100644 --- a/pineappl_fastnlo/src/fastnlo.hpp +++ b/pineappl_fastnlo/src/fastnlo.hpp @@ -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); @@ -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, diff --git a/pineappl_fastnlo/src/lib.rs b/pineappl_fastnlo/src/lib.rs index bea321c9..3e6bd604 100644 --- a/pineappl_fastnlo/src/lib.rs +++ b/pineappl_fastnlo/src/lib.rs @@ -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; @@ -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,