Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix small errors in examples/ code #6

Merged
merged 1 commit into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions examples/find.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ my_findfirst(p, haystack) = my_findnext(p, haystack, firstindex(haystack))
# When the predicate is looking for a single byte, we dispatch to see
# if the haystack is a chunk of memory. In that case, we can use memchr
function my_findnext(p::Base.Fix2{<:Union{typeof(==), typeof(isequal)}, UInt8}, haystack, k)
_my_findnext(MemoryKind(haystack), p, haystack, k)
_my_findnext(MemoryKind(typeof(haystack)), p, haystack, k)
end

# The generic: Use fallback
my_findnext(p, haystack, i) = _my_findnext(p, haystack, i)

# Default fallback
function my_findnext(p, haystack, i)
function _my_findnext(p, haystack, i)
lst = lastindex(haystack)
while i ≤ lst
p(haystack[i]) && return i
Expand Down Expand Up @@ -45,9 +48,9 @@ function my_findnext(
nothing
end

# Fallback - if the haystack is not IsMemory of bytes, we invoke the fallback definiion
# Fallback - if the haystack is not IsMemory of bytes, we use the fallback definiion
function _my_findnext(::MemoryKind, p, haystack, i)
@invoke my_findnext(p::Any, haystack::Any, i::Any)
_my_findnext(p, haystack, i)
end

# If it is bytes, we can convert the haystack to an ImmutableMemoryView,
Expand Down
7 changes: 4 additions & 3 deletions src/MemoryViews.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,19 @@ Convert a memory view into a mutable memory view.
Note that it may cause undefined behaviour, if supposedly immutable data
is observed to be mutated.
"""
MutableMemoryView(::Unsafe, x::MemoryView{T}) where {T} =
function MutableMemoryView(::Unsafe, x::MemoryView{T}) where {T}
MutableMemoryView{T}(unsafe, x.ref, x.len)
end

# Constructors that allows users to specify eltype explicitly, e.g.
# ImmutableMemoryView{UInt8}([0x01])
# With mutability specified
function MemoryView{T, M}(x) where {T, M}
(MemoryView{X, M} where X)(x)::MemoryView{T, M}
(MemoryView{X, M} where {X})(x)::MemoryView{T, M}
end

# With mutability unspecified
function MemoryView{T}(x) where T
function MemoryView{T}(x) where {T}
MemoryView(x)::MemoryView{T}
end

Expand Down
4 changes: 2 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ end
@test codeunits(s) == MemoryView(s)
@test codeunits(s[2:(end - 2)]) == MemoryView(s)[2:(end - 1)]

x = [1,2,3]
x = [1, 2, 3]
@test MemoryView{Int}(x) isa MutableMemoryView{Int}
@test ImmutableMemoryView{Int}(x) isa ImmutableMemoryView{Int}
@test_throws TypeError MemoryView{UInt32}(x)
Expand All @@ -81,7 +81,7 @@ end
end

@testset "Mightalias" begin
v = [1,2,3,4,5]
v = [1, 2, 3, 4, 5]
m1 = MemoryView(v)[2:3]
@test Base.mightalias(m1, MemoryView(v)[1:2])
@test Base.mightalias(m1, MemoryView(v)[3:4])
Expand Down
Loading