Skip to content

Define traits for table access and iteration #649

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 30 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
68efae8
new table access trait
molpopgen Jul 11, 2024
1d81517
make trait pub, add to prelude
molpopgen Jul 11, 2024
948cfea
new trait
molpopgen Jul 11, 2024
327f441
add basic test
molpopgen May 17, 2025
e8c567d
trait not object safe
molpopgen Jul 11, 2024
bfd59c1
object safety, at the cost of an indirection
molpopgen Jul 11, 2024
b894142
lint
molpopgen Jul 11, 2024
01e43fe
object safety, flexibility, but what about ergonomics?
molpopgen Jul 11, 2024
83ff8ce
fmt
molpopgen Jul 11, 2024
f743bd3
some fixes
molpopgen Jul 11, 2024
4912083
missed a table
molpopgen Jul 11, 2024
7b8608e
clippy gotta clip
molpopgen Jul 11, 2024
2a2dca1
up msrv to 1.75.0
molpopgen Jul 12, 2024
4e7789e
add tests. starting to repeat ourselves
molpopgen Jul 12, 2024
4b3b3bf
DRY in the tests
molpopgen Jul 12, 2024
afa9bbc
more test
molpopgen Jul 12, 2024
80d6f74
boil more plates
molpopgen Jul 12, 2024
ecbd751
more setup
molpopgen Jul 12, 2024
f18dd26
more setup
molpopgen Jul 12, 2024
201fbcc
fill in tests
molpopgen Jul 12, 2024
5423b75
do we need this
molpopgen Jul 12, 2024
edd29f3
streamline
molpopgen Jul 12, 2024
f326978
okay, we have other things that we do need to test
molpopgen Jul 12, 2024
17fefe9
test new pattern
molpopgen Jul 13, 2024
388f3ee
one down
molpopgen Jul 13, 2024
1dc3f9f
done
molpopgen Jul 13, 2024
3d238f6
scopes
molpopgen Jul 13, 2024
938a094
rewrite test to test the prelude contents
molpopgen Jul 14, 2024
43b7bb1
fix tests, fill in impl details
molpopgen Jul 15, 2024
90bc821
remove unused import
molpopgen May 17, 2025
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 .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ jobs:
strategy:
matrix:
rust:
- 1.71.0
- 1.75.0
steps:
- uses: actions/[email protected]
- uses: dtolnay/rust-toolchain@v1
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ description = "rust interface to tskit"
license = "MIT"
homepage = "https://github.com/tskit-dev/tskit-rust"
repository = "https://github.com/tskit-dev/tskit-rust"
rust-version = "1.71.0"
rust-version = "1.75.0"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lints.rust]
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ pub use table_collection::TableCollection;
pub use table_column::{EdgeTableColumn, NodeTableColumn};
pub use traits::IndividualLocation;
pub use traits::IndividualParents;
pub use traits::ObjectSafeTableIteration;
pub use traits::TableAccess;
pub use traits::TableIteration;
pub use trees::{Tree, TreeSequence};

// Optional features
Expand Down
2 changes: 2 additions & 0 deletions src/prelude.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Export commonly-use types and traits

pub use crate::TableAccess;
pub use crate::TableIteration;
pub use streaming_iterator::DoubleEndedStreamingIterator;
pub use streaming_iterator::StreamingIterator;
pub use {
Expand Down
161 changes: 161 additions & 0 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,164 @@ impl_individual_parents!(
);
impl_individual_parents!(N, usize, &[crate::IndividualId; N], self, self.as_slice());
impl_individual_parents!(N, usize, [crate::IndividualId; N], self, self.as_slice());

pub trait TableAccess {
fn edges(&self) -> &crate::EdgeTable;
fn nodes(&self) -> &crate::NodeTable;
fn sites(&self) -> &crate::SiteTable;
fn mutations(&self) -> &crate::MutationTable;
fn migrations(&self) -> &crate::MigrationTable;
fn populations(&self) -> &crate::PopulationTable;
}

pub trait TableIteration: TableAccess {
fn edges_iter(&self) -> impl Iterator<Item = crate::EdgeTableRow> + '_ {
self.edges().iter()
}
fn nodes_iter(&self) -> impl Iterator<Item = crate::NodeTableRow> + '_ {
self.nodes().iter()
}
fn sites_iter(&self) -> impl Iterator<Item = crate::SiteTableRow> + '_ {
self.sites().iter()
}
fn mutations_iter(&self) -> impl Iterator<Item = crate::MutationTableRow> + '_ {
self.mutations().iter()
}
fn migrations_iter(&self) -> impl Iterator<Item = crate::MigrationTableRow> + '_ {
self.migrations().iter()
}
fn populations_iter(&self) -> impl Iterator<Item = crate::PopulationTableRow> + '_ {
self.populations().iter()
}
}

pub trait ObjectSafeTableIteration: TableAccess {
fn edges_iter(&self) -> Box<dyn Iterator<Item = crate::EdgeTableRow> + '_> {
Box::new(self.edges().iter())
}
fn nodes_iter(&self) -> Box<dyn Iterator<Item = crate::NodeTableRow> + '_> {
Box::new(self.nodes().iter())
}
fn sites_iter(&self) -> Box<dyn Iterator<Item = crate::SiteTableRow> + '_> {
Box::new(self.sites().iter())
}
fn mutations_iter(&self) -> Box<dyn Iterator<Item = crate::MutationTableRow> + '_> {
Box::new(self.mutations().iter())
}
fn migrations_iter(&self) -> Box<dyn Iterator<Item = crate::MigrationTableRow> + '_> {
Box::new(self.migrations().iter())
}
fn populations_iter(&self) -> Box<dyn Iterator<Item = crate::PopulationTableRow> + '_> {
Box::new(Box::new(self.populations().iter()))
}
}

impl TableAccess for crate::TableCollection {
fn edges(&self) -> &crate::EdgeTable {
self.edges()
}

fn nodes(&self) -> &crate::NodeTable {
self.nodes()
}

fn sites(&self) -> &crate::SiteTable {
self.sites()
}

fn mutations(&self) -> &crate::MutationTable {
self.mutations()
}

fn migrations(&self) -> &crate::MigrationTable {
self.migrations()
}

fn populations(&self) -> &crate::PopulationTable {
self.populations()
}
}

impl TableAccess for crate::TreeSequence {
fn edges(&self) -> &crate::EdgeTable {
self.tables().edges()
}

fn nodes(&self) -> &crate::NodeTable {
self.tables().nodes()
}

fn sites(&self) -> &crate::SiteTable {
self.tables().sites()
}

fn mutations(&self) -> &crate::MutationTable {
self.tables().mutations()
}

fn migrations(&self) -> &crate::MigrationTable {
self.tables().migrations()
}

fn populations(&self) -> &crate::PopulationTable {
self.tables().populations()
}
}

impl<T> TableAccess for &T
where
T: TableAccess,
{
fn migrations(&self) -> &crate::MigrationTable {
T::migrations(self)
}

fn mutations(&self) -> &crate::MutationTable {
T::mutations(self)
}

fn edges(&self) -> &crate::EdgeTable {
T::edges(self)
}
fn sites(&self) -> &crate::SiteTable {
T::sites(self)
}
fn nodes(&self) -> &crate::NodeTable {
T::nodes(self)
}
fn populations(&self) -> &crate::PopulationTable {
T::populations(self)
}
}

impl<T> TableAccess for Box<T>
where
T: TableAccess,
{
fn migrations(&self) -> &crate::MigrationTable {
self.as_ref().migrations()
}

fn edges(&self) -> &crate::EdgeTable {
self.as_ref().edges()
}

fn nodes(&self) -> &crate::NodeTable {
self.as_ref().nodes()
}

fn sites(&self) -> &crate::SiteTable {
self.as_ref().sites()
}

fn mutations(&self) -> &crate::MutationTable {
self.as_ref().mutations()
}

fn populations(&self) -> &crate::PopulationTable {
self.as_ref().populations()
}
}

impl<T> TableIteration for T where T: TableAccess + ?Sized {}
impl<T> ObjectSafeTableIteration for T where T: TableAccess + ?Sized {}
Loading
Loading