Skip to content

Commit 8de1625

Browse files
committed
default
1 parent abd5c34 commit 8de1625

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ pub use individual_table::{IndividualTable, IndividualTableRow};
429429
pub use migration_table::{MigrationTable, MigrationTableRow};
430430
pub use mutation_table::{MutationTable, MutationTableRow};
431431
pub use node_table::{NodeTable, NodeTableRow};
432-
pub use population_table::{PopulationTable, PopulationTableRow};
432+
pub use population_table::{OwnedPopulationTable, PopulationTable, PopulationTableRow};
433433
pub use site_table::{SiteTable, SiteTableRow};
434434
pub use table_collection::TableCollection;
435435
pub use traits::IndividualLocation;

src/population_table.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,34 @@ impl<'a> PopulationTable<'a> {
116116
}
117117
}
118118

119+
/// A standalone population table that owns its data.
120+
///
121+
/// # Examples
122+
///
123+
/// ```
124+
/// use tskit::OwnedPopulationTable;
125+
///
126+
/// let mut populations = OwnedPopulationTable::default();
127+
/// ```
119128
pub struct OwnedPopulationTable {
120129
table: mbox::MBox<ll_bindings::tsk_population_table_t>,
121130
}
122131

132+
impl OwnedPopulationTable {
133+
fn new() -> Self {
134+
let temp = unsafe {
135+
libc::malloc(std::mem::size_of::<ll_bindings::tsk_population_table_t>())
136+
as *mut ll_bindings::tsk_population_table_t
137+
};
138+
let nonnull = match std::ptr::NonNull::<ll_bindings::tsk_population_table_t>::new(temp) {
139+
Some(x) => x,
140+
None => panic!("out of memory"),
141+
};
142+
let table = unsafe { mbox::MBox::from_non_null_raw(nonnull) };
143+
Self { table }
144+
}
145+
}
146+
123147
impl std::ops::Deref for OwnedPopulationTable {
124148
type Target = PopulationTable<'static>;
125149

@@ -128,3 +152,9 @@ impl std::ops::Deref for OwnedPopulationTable {
128152
unsafe { std::mem::transmute(&self.table) }
129153
}
130154
}
155+
156+
impl Default for OwnedPopulationTable {
157+
fn default() -> Self {
158+
Self::new()
159+
}
160+
}

0 commit comments

Comments
 (0)