Skip to content
This repository was archived by the owner on Feb 23, 2024. It is now read-only.

Commit 933ae10

Browse files
committed
Many API cleanups.
Fix a future-incompatible warning in some `io_util` macros due to trailing semicolons. Also fix those macros to work properly when expanded in contexts where the types they're accessing aren't in scope, and allow them to work with `impl AsRef<Path>` types rather than just `&String`. Add trait implementations where appropriate, mostly `Default` but also a few for `From` where it looked like it made sense given existing conversion functions, in order to bring the APIs into conformance with common naming conventions. Fix a bunch of clippy lints around stuff that looks like ```rust if f.is_err() { // thing } else { let f = f.unwrap(); // other thing } ``` using `match` instead to avoid checking more than once whether the type was an error. Replace uses of `&Vec<T>` with `&[T]` where possible. In some cases, the extent to which this was possible is limited until some of the upstream crates (from this repository) push a new version with their APIs updated. Replace uses of `&String` with either `&str` or `impl AsRef<Path>` as appropriate for the use case. Again, as with the `&Vec<T>` changes, in some cases this means temporarily going through fallible steps allocating String objects until the upstream crates publish versions with updated APIs. I could be wrong but I believe that `cargo clippy --fix` will be able to fix the TODOs from the above two items automatically when the time comes. The above two changes _shouldn't_ affect existing users, at least in most cases (`&Vec<T>` casts implicitly to `&[T]`, `&String` casts implicitly to `&str`, and both `&String` and `&str` implement `AsRef<Path>`). At least, in theory. Change the `segtype` field of `vdj_ann::refx::RefData` from a vector of owned `String` to a `&'static str`, since everything added to that vector is a constant value. This should significantly reduce memory consumption, as the vector is now a vector of pointers into constant memory rather than a vector of pointers each into their own heap- allocated copy of the source strings. Because that field is public, this is a breaking API change (though I don't know how many downstream users actually access the field directly), so I'm bumping the version of that crate. In vdj_ann_ref, don't convert `&'static str` to `String` except in cases where (similarly to above) we have to in order to satisfy the old APIs. Also, change the if-chaining logic to `match` statements to make the different cases more clear.
1 parent 96c18b8 commit 933ae10

File tree

40 files changed

+563
-520
lines changed

40 files changed

+563
-520
lines changed

align_tools/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "align_tools"
3-
version = "0.1.9"
3+
version = "0.1.10"
44
authors = ["David Jaffe <[email protected]>"]
55
license = "MIT"
66
description = "Some tools that are 'internal' for now because they are insufficiently refined and unstable, but which are used by other 'public' crates."

align_tools/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ pub fn affine_align(x: &DnaString, y: &DnaString) -> Alignment {
198198
// are used and would need to be tweaked if other operations are present. You can set width to
199199
// the expected terminal width.
200200

201-
pub fn vis_align(s1: &[u8], s2: &[u8], ops: &Vec<AlignmentOperation>, width: usize) -> String {
201+
pub fn vis_align(s1: &[u8], s2: &[u8], ops: &[AlignmentOperation], width: usize) -> String {
202202
let (mut pos1, mut pos2) = (0, 0);
203203
let (mut t1, mut t2) = (Vec::<u8>::new(), Vec::<u8>::new());
204204
let mut d = Vec::<u8>::new();

ansi_escape/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "ansi_escape"
3-
version = "0.1.2"
3+
version = "0.1.3"
44
authors = ["David Jaffe <[email protected]>"]
55
license = "MIT"
66
description = "Some tools that are 'internal' for now because they are insufficiently refined and unstable, but which are used by other 'public' crates."

ansi_escape/src/ansi_to_html.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,11 @@ pub fn convert_text_with_ansi_escapes_to_svg(
5555

5656
let lines0 = x.split('\n').collect::<Vec<&str>>();
5757
let height = vsep * (lines0.len() as f64 - 1.2);
58-
let mut lines = Vec::<String>::new();
59-
lines.push("<svg version=\"1.1\"".to_string());
60-
lines.push("".to_string()); // PLACEHOLDER
61-
lines.push("xmlns=\"http://www.w3.org/2000/svg\">".to_string());
58+
let mut lines = vec![
59+
"<svg version=\"1.1\"".to_string(),
60+
String::default(), // PLACEHOLDER
61+
"xmlns=\"http://www.w3.org/2000/svg\">".to_string(),
62+
];
6263
let mut max_width = 0;
6364
for m in 0..lines0.len() {
6465
let t = &lines0[m];
@@ -211,8 +212,8 @@ pub fn compress_ansi_escapes(x: &str) -> String {
211212
end = Some(i);
212213
}
213214
}
214-
if end.is_some() {
215-
escapes = escapes[end.unwrap() + 1..escapes.len()].to_vec();
215+
if let Some(end) = end {
216+
escapes = escapes[end + 1..escapes.len()].to_vec();
216217
}
217218
if escapes.is_empty() {
218219
// Emit end escape.
@@ -431,7 +432,7 @@ impl ColorState {
431432

432433
// ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
433434

434-
fn merge(s: &Vec<ColorState>) -> ColorState {
435+
fn merge(s: &[ColorState]) -> ColorState {
435436
let mut x = ColorState::default();
436437
for i in 0..s.len() {
437438
if s[i].null() {

binary_vec_io/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "binary_vec_io"
3-
version = "0.1.11"
3+
version = "0.1.12"
44
authors = ["David Jaffe <[email protected]>"]
55
license = "MIT"
66
description = "Some tools that are 'internal' for now because they are insufficiently refined and unstable, but which are used by other 'public' crates."

binary_vec_io/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ pub fn binary_read_to_ref<T>(f: &mut std::fs::File, p: &mut T, n: usize) -> Resu
7676
// in the first case, or to a vector, in the second case.
7777

7878
#[allow(dead_code)]
79-
pub fn binary_write_vec<T>(f: &mut std::fs::File, x: &Vec<T>) -> Result<(), Error>
79+
pub fn binary_write_vec<T>(f: &mut std::fs::File, x: &[T]) -> Result<(), Error>
8080
where
8181
T: BinaryInputOutputSafe,
8282
{
@@ -112,7 +112,7 @@ where
112112
binary_read_to_ref::<T>(f, &mut x[len], n)
113113
}
114114

115-
pub fn binary_write_vec_vec<T>(f: &mut std::fs::File, x: &Vec<Vec<T>>) -> Result<(), Error>
115+
pub fn binary_write_vec_vec<T>(f: &mut std::fs::File, x: &[Vec<T>]) -> Result<(), Error>
116116
where
117117
T: BinaryInputOutputSafe,
118118
{

dna/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "dna"
3-
version = "0.1.2"
3+
version = "0.1.3"
44
authors = ["David Jaffe <[email protected]>"]
55
license = "MIT"
66
description = "Some tools that are 'internal' for now because they are insufficiently refined and unstable, but which are used by other 'public' crates."

dna/src/lib.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,16 @@ pub fn tm_nearest_neighbor(s: &str) -> f64 {
4444
tm_nearest_neighbor_full(s, 0.00000025, 0.05, &locked)
4545
}
4646

47-
pub fn tm_nearest_neighbor_full(s: &str, s_mol: f64, na_mol: f64, locked: &Vec<bool>) -> f64 {
47+
pub fn tm_nearest_neighbor_full(s: &str, s_mol: f64, na_mol: f64, locked: &[bool]) -> f64 {
4848
// Allow for + symbols.
4949

5050
if s.contains('+') {
5151
assert_eq!(locked.len(), 0);
5252
assert!(!s.contains("++"));
5353
assert!(!s.ends_with('+'));
5454
let mut sx = String::new();
55-
let mut lockedx = Vec::<bool>::new();
56-
let mut schars = Vec::<char>::new();
57-
for c in s.chars() {
58-
schars.push(c);
59-
}
55+
let schars: Vec<char> = s.chars().collect();
56+
let mut lockedx = Vec::<bool>::with_capacity(schars.len());
6057
let mut i = 0;
6158
while i < schars.len() {
6259
if schars[i] != '+' {
@@ -365,7 +362,7 @@ pub fn thermodynamic_sums_dna(
365362
dg_sum: &mut f64,
366363
include_symmetry_correction: bool,
367364
include_initiation_terms: bool,
368-
locked: &Vec<bool>,
365+
locked: &[bool],
369366
) {
370367
// defaults for last: true, true, empty
371368

exons/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "exons"
3-
version = "0.1.4"
3+
version = "0.1.5"
44
authors = ["David Jaffe <[email protected]"]
55
license = "MIT"
66
description = "Some tools that are 'internal' for now because they are insufficiently refined and unstable, but which are used by other 'public' crates."

exons/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use std::{
1414
use string_utils::TextUtils;
1515
use vector_utils::unique_sort;
1616

17-
pub fn fetch_exons(species: &String, exons: &mut Vec<(String, i32, i32, bool, String, i32)>) {
17+
pub fn fetch_exons(species: &str, exons: &mut Vec<(String, i32, i32, bool, String, i32)>) {
1818
assert!(species == "human" || species == "mouse");
1919

2020
// Define gtf file location. See notes in bin/build_vdj_ref.fs.

expr_tools/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "expr_tools"
3-
version = "0.1.1"
3+
version = "0.1.2"
44
authors = ["David Jaffe <[email protected]>"]
55
license = "MIT"
66
description = "Some tools that are 'internal' for now because they are insufficiently refined and unstable, but which are used by other 'public' crates."

expr_tools/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use evalexpr::{ContextWithMutableFunctions, ContextWithMutableVariables, HashMapContext};
99
use evalexpr::{Function, Value};
1010
use statrs::distribution::ContinuousCDF;
11-
use string_utils::*;
11+
use string_utils::TextUtils;
1212
use vector_utils::{bin_member, unique_sort};
1313

1414
// ================================================================================================
@@ -124,7 +124,7 @@ macro_rules! evalexpr_fn3 {
124124
// to be called on arbitrary strings, but if the strings are not all f64, then the return value is
125125
// null.
126126

127-
pub fn define_evalexpr_context(vars: &Vec<String>, vals: &Vec<String>) -> evalexpr::HashMapContext {
127+
pub fn define_evalexpr_context(vars: &[String], vals: &[String]) -> evalexpr::HashMapContext {
128128
assert_eq!(vars.len(), vals.len());
129129
let mut c = HashMapContext::new();
130130

fasta_tools/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "fasta_tools"
3-
version = "0.1.5"
3+
version = "0.1.6"
44
authors = ["David Jaffe <[email protected]>"]
55
license = "MIT"
66
description = "Some tools that are 'internal' for now because they are insufficiently refined and unstable, but which are used by other 'public' crates."

0 commit comments

Comments
 (0)