mpi_fork_fnsp/raw.rs
1//! Bridge between rust types and raw values
2#![allow(clippy::module_name_repetitions)]
3/// Rust C bridge traits
4pub mod traits {
5 pub use super::{AsRaw, AsRawMut, FromRaw, MatchesRaw};
6}
7
8/// A rust type than can identify as a raw value understood by the MPI C API.
9pub unsafe trait AsRaw {
10 /// The raw MPI C API type
11 type Raw;
12 /// The raw value
13 fn as_raw(&self) -> Self::Raw;
14}
15
16unsafe impl<'a, T> AsRaw for &'a T
17where
18 T: 'a + AsRaw,
19{
20 type Raw = <T as AsRaw>::Raw;
21 fn as_raw(&self) -> Self::Raw {
22 (*self).as_raw()
23 }
24}
25
26/// A rust type than can provide a mutable pointer to a raw value understood by the MPI C API.
27pub unsafe trait AsRawMut: AsRaw {
28 /// A mutable pointer to the raw value
29 fn as_raw_mut(&mut self) -> *mut <Self as AsRaw>::Raw;
30}
31
32/// Conversion for the Rust type from the raw MPI handle type.
33pub trait FromRaw: AsRaw {
34 /// Constructs the Rust type, with all its semantics, from the raw MPI type.
35 ///
36 /// # Safety
37 /// `handle` may be assumed to be a live MPI object.
38 unsafe fn from_raw(handle: <Self as AsRaw>::Raw) -> Self;
39}
40
41/// A marker trait that indicates the Rust type is exactly equivalent in representation to the Raw
42/// type, allowing slices of the type to be used with MPI APIs that accept arrays of its Raw MPI
43/// handle.
44pub unsafe trait MatchesRaw: AsRaw {}