Skip to main content

RawSortKey

Trait RawSortKey 

Source
pub trait RawSortKey:
    Ord
    + Clone
    + Send
    + Sync
    + Sized {
    const SERIALIZED_SIZE: Option<usize>;

    // Required methods
    fn extract(bam: &[u8], ctx: &SortContext) -> Self;
    fn write_to<W: Write>(&self, writer: &mut W) -> Result<()>;
    fn read_from<R: Read>(reader: &mut R) -> Result<Self>;
}
Expand description

Trait for sort keys extracted from raw BAM bytes.

This trait provides a unified interface for all sort key types, enabling:

  • Zero-copy key extraction from raw BAM bytes
  • Efficient serialization for temp file storage
  • O(1) comparisons during merge phase (for fixed-size keys)

§Design

Inspired by fgbio’s SamOrder trait and samtools’ bam1_tag approach. Using a trait with monomorphization (sort_with_keyed<K>) gives:

  • Zero-cost abstraction (no runtime dispatch)
  • Type safety (can’t mix key types)
  • Compile-time optimization (inlining, etc.)

Required Associated Constants§

Source

const SERIALIZED_SIZE: Option<usize>

Fixed byte size when serialized, or None for variable-length keys.

Fixed-size keys enable O(1) reads from temp files during merge.

Required Methods§

Source

fn extract(bam: &[u8], ctx: &SortContext) -> Self

Extract a sort key from raw BAM record bytes.

This is the hot path during sorting - implementations should minimize parsing overhead by reading only the fields needed for comparison.

Source

fn write_to<W: Write>(&self, writer: &mut W) -> Result<()>

Serialize the key to a writer for temp file storage.

Format should be compact and enable fast deserialization.

§Errors

Returns an error if writing to the writer fails.

Source

fn read_from<R: Read>(reader: &mut R) -> Result<Self>

Deserialize a key from a reader.

Must be the inverse of write_to.

§Errors

Returns an error if reading from the reader fails or data is invalid.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§