Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ pub use sys::NodeTraversalOrder;
pub use table_collection::TableCollection;
pub use traits::IndividualLocation;
pub use traits::IndividualParents;
pub use traits::TableColumn;
pub use traits::{IterableTableColumn, TableColumn};
pub use trees::{Tree, TreeSequence};

pub use sys::Edge;
Expand Down
32 changes: 23 additions & 9 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,6 @@ pub trait TableColumn<I, T>:
self.as_slice().get(usize::try_from(at).ok()?)
}

/// Iterator over the data.
fn iter<'a, 'b>(&'a self) -> impl Iterator<Item = &'b T>
where
'a: 'b,
T: 'b,
{
self.as_slice().iter()
}

/// Column length
fn len(&self) -> usize {
self.as_slice().len()
Expand All @@ -203,6 +194,19 @@ pub trait TableColumn<I, T>:
}
}

pub trait IterableTableColumn<I, T>: TableColumn<I, T> {
/// Iterator over the data.
fn iter<'a, 'b>(&'a self) -> impl Iterator<Item = &'b T>
where
'a: 'b,
T: 'b,
{
self.as_slice().iter()
}
}

impl<I, T, U: TableColumn<I, T>> IterableTableColumn<I, T> for U {}

impl<T> private::TableColumnMarker for crate::table_column::OpaqueTableColumn<'_, T> {}

impl<I, T> std::ops::Index<I> for crate::table_column::OpaqueTableColumn<'_, T>
Expand All @@ -227,3 +231,13 @@ where
self.0.get(at.try_into().ok()?)
}
}

#[test]
fn test_dyn_compatibility() {
fn is_dyn(t: &dyn TableColumn<crate::NodeId, crate::IndividualId>) -> u64 {
t.as_slice().iter().count() as u64
}

let n = crate::NodeTable::default();
assert_eq!(n.num_rows(), is_dyn(&n.individual_column()));
}
1 change: 1 addition & 0 deletions tests/test_tables.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use tskit::IterableTableColumn;
use tskit::TableColumn;

#[test]
Expand Down
Loading