From 21084032a351ea226bd0c574f9e6fbe42686c3d9 Mon Sep 17 00:00:00 2001 From: Shaun Jackman Date: Thu, 4 May 2023 17:00:50 -0700 Subject: [PATCH] feat: HeaderRecord::push_tag: Value may be owned The type of the value may be a ref or owned. Previously it must be a ref. --- src/bam/header.rs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/bam/header.rs b/src/bam/header.rs index eef5b79d9..b8a6a25e7 100644 --- a/src/bam/header.rs +++ b/src/bam/header.rs @@ -139,7 +139,7 @@ impl<'a> HeaderRecord<'a> { /// * `tag` - the tag identifier /// * `value` - the value. Can be any type convertible into a string. Preferably numbers or /// strings. - pub fn push_tag(&mut self, tag: &'a [u8], value: &V) -> &mut Self { + pub fn push_tag(&mut self, tag: &'a [u8], value: V) -> &mut Self { self.tags.push((tag, value.to_string().into_bytes())); self } @@ -156,3 +156,22 @@ impl<'a> HeaderRecord<'a> { out } } + +#[cfg(test)] +mod tests { + use super::HeaderRecord; + + #[test] + fn test_push_tag() { + let mut record = HeaderRecord::new(b"HD"); + record.push_tag(b"X1", 0); + record.push_tag(b"X2", &0); + + let x = "x".to_string(); + record.push_tag(b"X3", x.as_str()); + record.push_tag(b"X4", &x); + record.push_tag(b"X5", x); + + assert_eq!(record.to_bytes(), b"@HD\tX1:0\tX2:0\tX3:x\tX4:x\tX5:x"); + } +}