|
| 1 | +//! Define traits related to wrapping tskit stuff |
| 2 | +
|
| 3 | +/// Define what it means to wrap a tskit struct. |
| 4 | +/// The implementation of Drop should call the |
| 5 | +/// tsk_foo_free() function corresponding |
| 6 | +/// to tsk_foo_t. |
| 7 | +pub trait TskitType<T>: Drop { |
| 8 | + /// Encapsulate tsk_foo_t and return rust |
| 9 | + /// object. Best practices seem to |
| 10 | + /// suggest using Box for this. |
| 11 | + fn wrap() -> Self; |
| 12 | + /// Return const pointer |
| 13 | + fn as_ptr(&self) -> *const T; |
| 14 | + /// Return mutable pointer |
| 15 | + fn as_mut_ptr(&mut self) -> *mut T; |
| 16 | +} |
| 17 | + |
| 18 | +#[cfg(test)] |
| 19 | +mod tests { |
| 20 | + use super::*; |
| 21 | + use crate::bindings as ll_bindings; |
| 22 | + use ll_bindings::tsk_table_collection_free; |
| 23 | + |
| 24 | + pub struct TableCollectionMock { |
| 25 | + inner: Box<ll_bindings::tsk_table_collection_t>, |
| 26 | + } |
| 27 | + |
| 28 | + build_tskit_type!( |
| 29 | + TableCollectionMock, |
| 30 | + ll_bindings::tsk_table_collection_t, |
| 31 | + tsk_table_collection_free |
| 32 | + ); |
| 33 | + |
| 34 | + impl TableCollectionMock { |
| 35 | + fn new(len: f64) -> Self { |
| 36 | + let mut s = Self::wrap(); |
| 37 | + |
| 38 | + let rv = unsafe { ll_bindings::tsk_table_collection_init(s.as_mut_ptr(), 0) }; |
| 39 | + assert_eq!(rv, 0); |
| 40 | + |
| 41 | + s.inner.sequence_length = len; |
| 42 | + |
| 43 | + s |
| 44 | + } |
| 45 | + |
| 46 | + fn sequence_length(&self) -> f64 { |
| 47 | + unsafe { (*self.as_ptr()).sequence_length } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + #[test] |
| 52 | + fn test_create_mock_type() { |
| 53 | + let t = TableCollectionMock::new(10.); |
| 54 | + assert_eq!(t.sequence_length() as i64, 10); |
| 55 | + } |
| 56 | +} |
0 commit comments