Skip to content

Commit 89d234f

Browse files
authored
Replace WrappedTskArray with idiomatic slices. Closes #99 (#101)
1 parent ed8329b commit 89d234f

File tree

3 files changed

+245
-158
lines changed

3 files changed

+245
-158
lines changed

src/_macros.rs

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -168,28 +168,6 @@ macro_rules! process_state_input {
168168
};
169169
}
170170

171-
macro_rules! index_for_wrapped_tsk_array_type {
172-
($name: ty, $index:ty, $output: ty) => {
173-
impl std::ops::Index<$index> for $name {
174-
type Output = $output;
175-
176-
fn index(&self, index: $index) -> &Self::Output {
177-
if index >= self.len() as $index {
178-
panic!("fatal: index out of range");
179-
}
180-
let rv = unsafe { self.array.offset(index as isize) };
181-
unsafe { &*rv }
182-
}
183-
}
184-
};
185-
}
186-
187-
macro_rules! wrapped_tsk_array_traits {
188-
($name: ty, $index:ty, $output: ty) => {
189-
index_for_wrapped_tsk_array_type!($name, $index, $output);
190-
};
191-
}
192-
193171
macro_rules! err_if_not_tracking_samples {
194172
($flags: expr, $rv: expr) => {
195173
match $flags.contains(crate::TreeFlags::SAMPLE_LISTS) {
@@ -228,6 +206,14 @@ macro_rules! iterator_for_nodeiterator {
228206
};
229207
}
230208

209+
macro_rules! tree_array_slice {
210+
($self: ident, $array: ident, $len: expr) => {
211+
unsafe {
212+
std::slice::from_raw_parts((*$self.as_ptr()).$array as *const tsk_id_t, $len as usize)
213+
}
214+
};
215+
}
216+
231217
#[cfg(test)]
232218
mod test {
233219
use crate::error::TskitError;

src/ffi.rs

Lines changed: 0 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,5 @@
11
//! Define traits related to wrapping tskit stuff
22
3-
/// Indexable, iterable wrapper around C
4-
/// arrays.
5-
#[derive(Copy, Clone)]
6-
pub struct WrappedTskArray<T> {
7-
array: *const T,
8-
len_: crate::tsk_size_t,
9-
}
10-
11-
pub struct WrappedTskArrayIter<'a, T: Copy + 'a> {
12-
inner: &'a WrappedTskArray<T>,
13-
pos: crate::tsk_size_t,
14-
}
15-
16-
impl<'a, T: Copy> Iterator for WrappedTskArrayIter<'a, T> {
17-
type Item = T;
18-
19-
fn next(&mut self) -> Option<Self::Item> {
20-
if self.pos >= self.inner.len_ {
21-
None
22-
} else {
23-
let rv = Some(unsafe { *self.inner.array.offset(self.pos as isize) as T });
24-
self.pos += 1;
25-
rv
26-
}
27-
}
28-
}
29-
30-
impl<T: Copy> WrappedTskArray<T> {
31-
pub(crate) fn new(array: *const T, len: crate::tsk_size_t) -> Self {
32-
Self { array, len_: len }
33-
}
34-
35-
pub fn len(&self) -> crate::tsk_size_t {
36-
self.len_
37-
}
38-
39-
pub fn is_empty(&self) -> bool {
40-
self.len_ == 0
41-
}
42-
43-
/// # Safety
44-
///
45-
/// This function returns the raw C pointer,
46-
/// and is thus unsafe.
47-
pub fn as_ptr(&self) -> *const T {
48-
self.array
49-
}
50-
51-
pub fn iter(&self) -> WrappedTskArrayIter<T> {
52-
WrappedTskArrayIter {
53-
inner: self,
54-
pos: 0,
55-
}
56-
}
57-
}
58-
59-
pub(crate) type TskIdArray = WrappedTskArray<crate::tsk_id_t>;
60-
pub(crate) type Tskf64Array = WrappedTskArray<f64>;
61-
62-
wrapped_tsk_array_traits!(TskIdArray, crate::tsk_id_t, crate::tsk_id_t);
63-
wrapped_tsk_array_traits!(Tskf64Array, crate::tsk_id_t, f64);
64-
653
/// Wrap a tskit type
664
pub(crate) trait WrapTskitType<T> {
675
/// Encapsulate tsk_foo_t and return rust
@@ -83,7 +21,6 @@ pub(crate) trait WrapTskitConsumingType<T, C> {
8321
mod tests {
8422
use super::*;
8523
use crate::bindings as ll_bindings;
86-
use crate::tsk_size_t;
8724
use crate::TskitTypeAccess;
8825
use ll_bindings::tsk_table_collection_free;
8926

@@ -119,56 +56,4 @@ mod tests {
11956
let t = TableCollectionMock::new(10.);
12057
assert_eq!(t.sequence_length() as i64, 10);
12158
}
122-
123-
#[test]
124-
fn test_u32_array_wrapper() {
125-
let mut t = TableCollectionMock::new(10.);
126-
127-
let rv = unsafe {
128-
ll_bindings::tsk_edge_table_add_row(
129-
&mut (*t.as_mut_ptr()).edges,
130-
0.,
131-
10.,
132-
0,
133-
17,
134-
std::ptr::null(),
135-
0,
136-
)
137-
};
138-
panic_on_tskit_error!(rv);
139-
140-
let a = TskIdArray::new(unsafe { (*t.as_ptr()).edges.child }, 1);
141-
assert_eq!(a.len(), 1);
142-
assert_eq!(a[0], 17);
143-
144-
let mut v = vec![];
145-
for i in a.iter() {
146-
v.push(i);
147-
}
148-
assert_eq!(v.len() as tsk_size_t, a.len());
149-
assert_eq!(v[0], 17);
150-
}
151-
152-
#[should_panic]
153-
#[test]
154-
fn test_u32_array_wrapper_panic() {
155-
let mut t = TableCollectionMock::new(10.);
156-
157-
let rv = unsafe {
158-
ll_bindings::tsk_edge_table_add_row(
159-
&mut (*t.as_mut_ptr()).edges,
160-
0.,
161-
10.,
162-
0,
163-
17,
164-
std::ptr::null(),
165-
0,
166-
)
167-
};
168-
panic_on_tskit_error!(rv);
169-
170-
let a = TskIdArray::new(unsafe { (*t.as_ptr()).edges.child }, 1);
171-
assert_eq!(a.len(), 1);
172-
assert_eq!(a[1], 17);
173-
}
17459
}

0 commit comments

Comments
 (0)