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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//! Define traits related to wrapping tskit stuff

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

pub struct WrappedTskArrayIter<'a, T: Copy + 'a> {
    inner: &'a WrappedTskArray<T>,
    pos: crate::tsk_size_t,
}

impl<'a, T: Copy> Iterator for WrappedTskArrayIter<'a, T> {
    type Item = T;

    fn next(&mut self) -> Option<Self::Item> {
        if self.pos >= self.inner.len_ {
            None
        } else {
            let rv = Some(unsafe { *self.inner.array.offset(self.pos as isize) as T });
            self.pos += 1;
            rv
        }
    }
}

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

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

    pub fn is_empty(&self) -> bool {
        self.len_ == 0
    }

    /// # Safety
    ///
    /// This function returns the raw C pointer,
    /// and is thus unsafe.
    pub unsafe fn as_ptr(&self) -> *const T {
        self.array
    }

    pub fn iter(&self) -> WrappedTskArrayIter<T> {
        WrappedTskArrayIter {
            inner: self,
            pos: 0,
        }
    }
}

pub(crate) type TskIdArray = WrappedTskArray<crate::tsk_id_t>;
pub(crate) type Tskf64Array = WrappedTskArray<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 crate::tsk_size_t;
    use crate::TskitTypeAccess;
    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 a = TskIdArray::new(unsafe { (*t.as_ptr()).edges.child }, 1);
        assert_eq!(a.len(), 1);
        assert_eq!(a[0], 17);

        let mut v = vec![];
        for i in a.iter() {
            v.push(i);
        }
        assert_eq!(v.len() as tsk_size_t, a.len());
        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);
    }
}