Skip to content

Commit

Permalink
Add Unsafe trait object (#27)
Browse files Browse the repository at this point in the history
The `Unsafe` trait object allows access to unsafe methods.
  • Loading branch information
jakobnissen authored Jul 13, 2022
1 parent 320439b commit cb76fbe
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/kmer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@
# manipping Kmer's static data.
include("tuple_bitflipping.jl")

"""
Kmers.Unsafe
Trait object used to access unsafe methods of functions.
`unsafe` is the singleton of `Unsafe`.
"""
struct Unsafe end
const unsafe = Unsafe()

"""
Kmer{A<:Alphabet,K,N} <: BioSequence{A}
Expand Down Expand Up @@ -33,11 +42,11 @@ of the type are still defined.
struct Kmer{A<:Alphabet,K,N} <: BioSequence{A}
data::NTuple{N,UInt64}

# This unsafe method do not clip the head
Kmer{A,K,N}(::Unsafe, data::NTuple{N,UInt64}) where {A<:Alphabet,K,N} = new{A,K,N}(data)

function Kmer{A,K,N}(data::NTuple{N,UInt64}) where {A<:Alphabet,K,N}
checkmer(Kmer{A,K,N})
# TODO: Decide on whether this method should always mask the (64N - 2K)
# MSBs of the input tuple, as we do that in quite a few cases before
# calling this constructor: see the typemin, typemax, rand, and transformations.jl
x = n_unused(Kmer{A,K,N}) * BioSequences.bits_per_symbol(A())
return new(_cliphead(x, data...))
end
Expand Down Expand Up @@ -429,7 +438,7 @@ end
@inline Base.summary(x::Kmer{A,K,N}) where {A,K,N} = string(eltype(x), ' ', K, "-mer")

function Base.typemin(::Type{Kmer{A,K,N}}) where {A,K,N}
return Kmer{A,K,N}(ntuple(i -> zero(UInt64), N))
return Kmer{A,K,N}(unsafe, ntuple(i -> zero(UInt64), N))
end

function Base.typemax(::Type{Kmer{A,K,N}}) where {A,K,N}
Expand Down

0 comments on commit cb76fbe

Please sign in to comment.