@@ -127,25 +127,55 @@ impl TableCollection {
127
127
///
128
128
/// # Examples
129
129
///
130
+ /// The function is generic over references to `str`:
131
+ ///
130
132
/// ```
131
133
/// # let empty_tables = tskit::TableCollection::new(100.).unwrap();
132
134
/// # empty_tables.dump("trees.file", tskit::TableOutputOptions::default()).unwrap();
133
135
/// let tables = tskit::TableCollection::new_from_file("trees.file").unwrap();
134
136
/// # std::fs::remove_file("trees.file").unwrap();
135
137
/// ```
136
138
///
139
+ /// ```
140
+ /// # let empty_tables = tskit::TableCollection::new(100.).unwrap();
141
+ /// # empty_tables.dump("trees.file", tskit::TableOutputOptions::default()).unwrap();
142
+ /// let filename = String::from("trees.file");
143
+ /// // Move filename
144
+ /// let tables = tskit::TableCollection::new_from_file(filename).unwrap();
145
+ /// # std::fs::remove_file("trees.file").unwrap();
146
+ /// ```
147
+ ///
148
+ /// ```
149
+ /// # let empty_tables = tskit::TableCollection::new(100.).unwrap();
150
+ /// # empty_tables.dump("trees.file", tskit::TableOutputOptions::default()).unwrap();
151
+ /// let filename = String::from("trees.file");
152
+ /// // Pass filename by reference
153
+ /// let tables = tskit::TableCollection::new_from_file(&filename).unwrap();
154
+ /// # std::fs::remove_file("trees.file").unwrap();
155
+ /// ```
156
+ ///
157
+ /// Boxed `String`s are an unlikely use case, but can be made to work:
158
+ ///
159
+ /// ```
160
+ /// # let empty_tables = tskit::TableCollection::new(100.).unwrap();
161
+ /// # empty_tables.dump("trees.file", tskit::TableOutputOptions::default()).unwrap();
162
+ /// let filename = Box::new(String::from("trees.file"));
163
+ /// let tables = tskit::TableCollection::new_from_file(&*filename.as_ref()).unwrap();
164
+ /// # std::fs::remove_file("trees.file").unwrap();
165
+ /// ```
166
+ ///
137
167
/// # Panics
138
168
///
139
169
/// This function allocates a `CString` to pass the file name to the C API.
140
170
/// A panic will occur if the system runs out of memory.
141
- pub fn new_from_file ( filename : & str ) -> Result < Self , TskitError > {
171
+ pub fn new_from_file ( filename : impl AsRef < str > ) -> Result < Self , TskitError > {
142
172
// Arbitrary sequence_length.
143
173
let mut tables = match TableCollection :: new ( 1.0 ) {
144
174
Ok ( t) => ( t) ,
145
175
Err ( e) => return Err ( e) ,
146
176
} ;
147
177
148
- let c_str = std:: ffi:: CString :: new ( filename) . unwrap ( ) ;
178
+ let c_str = std:: ffi:: CString :: new ( filename. as_ref ( ) ) . unwrap ( ) ;
149
179
let rv = unsafe {
150
180
ll_bindings:: tsk_table_collection_load (
151
181
tables. as_mut_ptr ( ) ,
0 commit comments