Skip to content

feat: make load from file generic over AsRef<str> #254

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

Merged
merged 1 commit into from
Jul 14, 2022
Merged
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
34 changes: 32 additions & 2 deletions src/table_collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,25 +127,55 @@ impl TableCollection {
///
/// # Examples
///
/// The function is generic over references to `str`:
///
/// ```
/// # let empty_tables = tskit::TableCollection::new(100.).unwrap();
/// # empty_tables.dump("trees.file", tskit::TableOutputOptions::default()).unwrap();
/// let tables = tskit::TableCollection::new_from_file("trees.file").unwrap();
/// # std::fs::remove_file("trees.file").unwrap();
/// ```
///
/// ```
/// # let empty_tables = tskit::TableCollection::new(100.).unwrap();
/// # empty_tables.dump("trees.file", tskit::TableOutputOptions::default()).unwrap();
/// let filename = String::from("trees.file");
/// // Move filename
/// let tables = tskit::TableCollection::new_from_file(filename).unwrap();
/// # std::fs::remove_file("trees.file").unwrap();
/// ```
///
/// ```
/// # let empty_tables = tskit::TableCollection::new(100.).unwrap();
/// # empty_tables.dump("trees.file", tskit::TableOutputOptions::default()).unwrap();
/// let filename = String::from("trees.file");
/// // Pass filename by reference
/// let tables = tskit::TableCollection::new_from_file(&filename).unwrap();
/// # std::fs::remove_file("trees.file").unwrap();
/// ```
///
/// Boxed `String`s are an unlikely use case, but can be made to work:
///
/// ```
/// # let empty_tables = tskit::TableCollection::new(100.).unwrap();
/// # empty_tables.dump("trees.file", tskit::TableOutputOptions::default()).unwrap();
/// let filename = Box::new(String::from("trees.file"));
/// let tables = tskit::TableCollection::new_from_file(&*filename.as_ref()).unwrap();
/// # std::fs::remove_file("trees.file").unwrap();
/// ```
///
/// # Panics
///
/// This function allocates a `CString` to pass the file name to the C API.
/// A panic will occur if the system runs out of memory.
pub fn new_from_file(filename: &str) -> Result<Self, TskitError> {
pub fn new_from_file(filename: impl AsRef<str>) -> Result<Self, TskitError> {
// Arbitrary sequence_length.
let mut tables = match TableCollection::new(1.0) {
Ok(t) => (t),
Err(e) => return Err(e),
};

let c_str = std::ffi::CString::new(filename).unwrap();
let c_str = std::ffi::CString::new(filename.as_ref()).unwrap();
let rv = unsafe {
ll_bindings::tsk_table_collection_load(
tables.as_mut_ptr(),
Expand Down
7 changes: 5 additions & 2 deletions src/trees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1042,8 +1042,11 @@ impl TreeSequence {
}

/// Load from a file.
pub fn load(filename: &str) -> Result<Self, TskitError> {
let tables = TableCollection::new_from_file(filename)?;
///
/// This function calls [`TableCollection::new_from_file`] with
/// [`TreeSequenceFlags::default`].
pub fn load(filename: impl AsRef<str>) -> Result<Self, TskitError> {
let tables = TableCollection::new_from_file(filename.as_ref())?;

Self::new(tables, TreeSequenceFlags::default())
}
Expand Down