1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//! Define traits related to wrapping tskit stuff

/// Provide pointer access to underlying C types
pub trait TskitTypeAccess<T> {
    /// Return const pointer
    fn as_ptr(&self) -> *const T;
    /// Return mutable pointer
    fn as_mut_ptr(&mut self) -> *mut T;
}

/// Indexable, iterable wrapper around C
/// arrays.
#[derive(Copy, Clone)]
pub(crate) struct WrappedTskArray<T> {
    array: T,
    len_: crate::tsk_size_t,
    idx_: crate::tsk_id_t,
}

impl<T> WrappedTskArray<T> {
    pub fn new(array: T, len: crate::tsk_size_t) -> Self {
        Self {
            array,
            len_: len,
            idx_: -1,
        }
    }

    pub fn len(&self) -> crate::tsk_size_t {
        self.len_
    }
}

pub(crate) type TskIdArray = WrappedTskArray<*const crate::tsk_id_t>;
pub(crate) type Tskf64Array = WrappedTskArray<*const f64>;

wrapped_tsk_array_traits!(TskIdArray, crate::tsk_id_t, crate::tsk_id_t);
wrapped_tsk_array_traits!(Tskf64Array, crate::tsk_id_t, f64);

/// Wrap a tskit type
pub(crate) trait WrapTskitType<T> {
    /// Encapsulate tsk_foo_t and return rust
    /// object.  Best practices seem to
    /// suggest using Box for this.
    fn wrap() -> Self;
}

/// Wrap a tskit type that consumes another
/// tskit type.  The tree sequence is an example.
pub(crate) trait WrapTskitConsumingType<T, C> {
    /// Encapsulate tsk_foo_t and return rust
    /// object.  Best practices seem to
    /// suggest using Box for this.
    fn wrap(consumed: C) -> Self;
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::bindings as ll_bindings;
    use ll_bindings::tsk_table_collection_free;

    pub struct TableCollectionMock {
        inner: Box<ll_bindings::tsk_table_collection_t>,
    }

    build_tskit_type!(
        TableCollectionMock,
        ll_bindings::tsk_table_collection_t,
        tsk_table_collection_free
    );

    impl TableCollectionMock {
        fn new(len: f64) -> Self {
            let mut s = Self::wrap();

            let rv = unsafe { ll_bindings::tsk_table_collection_init(s.as_mut_ptr(), 0) };
            assert_eq!(rv, 0);

            s.inner.sequence_length = len;

            s
        }

        fn sequence_length(&self) -> f64 {
            unsafe { (*self.as_ptr()).sequence_length }
        }
    }

    #[test]
    fn test_create_mock_type() {
        let t = TableCollectionMock::new(10.);
        assert_eq!(t.sequence_length() as i64, 10);
    }

    #[test]
    fn test_u32_array_wrapper() {
        let mut t = TableCollectionMock::new(10.);

        let rv = unsafe {
            ll_bindings::tsk_edge_table_add_row(
                &mut (*t.as_mut_ptr()).edges,
                0.,
                10.,
                0,
                17,
                std::ptr::null(),
                0,
            )
        };
        panic_on_tskit_error!(rv);

        let mut a = TskIdArray::new(unsafe { (*t.as_ptr()).edges.child }, 1);
        assert_eq!(a.len(), 1);
        assert_eq!(a[0], 17);

        assert_eq!(a.next(), Some(17));
        assert_eq!(a.next(), None);

        let mut v = vec![];
        for i in &mut a {
            v.push(i);
        }
        assert_eq!(v.len(), 1);
        assert_eq!(v[0], 17);

        v = vec![];
        for i in &mut a {
            v.push(i);
        }
        assert_eq!(v.len(), 1);
        assert_eq!(v[0], 17);
    }

    #[should_panic]
    #[test]
    fn test_u32_array_wrapper_panic() {
        let mut t = TableCollectionMock::new(10.);

        let rv = unsafe {
            ll_bindings::tsk_edge_table_add_row(
                &mut (*t.as_mut_ptr()).edges,
                0.,
                10.,
                0,
                17,
                std::ptr::null(),
                0,
            )
        };
        panic_on_tskit_error!(rv);

        let a = TskIdArray::new(unsafe { (*t.as_ptr()).edges.child }, 1);
        assert_eq!(a.len(), 1);
        assert_eq!(a[1], 17);
    }
}