|
1 | | -use crate::bindings; |
| 1 | +use crate::{bindings, TskitError}; |
| 2 | +use bindings::tsk_edge_table_t; |
| 3 | +use bindings::tsk_individual_table_t; |
| 4 | +use bindings::tsk_migration_table_t; |
| 5 | +use bindings::tsk_mutation_table_t; |
| 6 | +use bindings::tsk_node_table_t; |
| 7 | +use bindings::tsk_population_table_t; |
| 8 | +use bindings::tsk_site_table_t; |
| 9 | +use std::ptr::NonNull; |
| 10 | + |
| 11 | +#[cfg(feature = "provenance")] |
| 12 | +use bindings::tsk_provenance_table_t; |
| 13 | + |
| 14 | +macro_rules! basic_lltableref_impl { |
| 15 | + ($lltable: ident, $tsktable: ident) => { |
| 16 | + #[repr(transparent)] |
| 17 | + #[derive(Debug)] |
| 18 | + pub struct $lltable(NonNull<bindings::$tsktable>); |
| 19 | + |
| 20 | + impl $lltable { |
| 21 | + pub fn new_from_table(table: *mut $tsktable) -> Result<Self, TskitError> { |
| 22 | + let internal = NonNull::new(table).ok_or_else(|| { |
| 23 | + let msg = format!("null pointer to {}", stringify!($tsktable)); |
| 24 | + TskitError::LibraryError(msg) |
| 25 | + })?; |
| 26 | + Ok(Self(internal)) |
| 27 | + } |
| 28 | + |
| 29 | + pub fn as_ref(&self) -> &$tsktable { |
| 30 | + // SAFETY: we cannot get this far w/o |
| 31 | + // going through new_from_table and that |
| 32 | + // fn protects us from null ptrs |
| 33 | + unsafe { self.0.as_ref() } |
| 34 | + } |
| 35 | + } |
| 36 | + }; |
| 37 | +} |
| 38 | + |
| 39 | +basic_lltableref_impl!(LLEdgeTableRef, tsk_edge_table_t); |
| 40 | +basic_lltableref_impl!(LLNodeTableRef, tsk_node_table_t); |
| 41 | +basic_lltableref_impl!(LLMutationTableRef, tsk_mutation_table_t); |
| 42 | +basic_lltableref_impl!(LLSiteTableRef, tsk_site_table_t); |
| 43 | +basic_lltableref_impl!(LLMigrationTableRef, tsk_migration_table_t); |
| 44 | +basic_lltableref_impl!(LLPopulationTableRef, tsk_population_table_t); |
| 45 | +basic_lltableref_impl!(LLIndividualTableRef, tsk_individual_table_t); |
| 46 | + |
| 47 | +#[cfg(feature = "provenance")] |
| 48 | +basic_lltableref_impl!(LLProvenanceTableRef, tsk_provenance_table_t); |
2 | 49 |
|
3 | 50 | fn tsk_column_access_detail<R: Into<bindings::tsk_id_t>, L: Into<bindings::tsk_size_t>, T: Copy>( |
4 | 51 | row: R, |
|
0 commit comments