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

Add constructor that allows specifying type #5

Merged
merged 1 commit into from
Nov 13, 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
12 changes: 12 additions & 0 deletions src/MemoryViews.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,18 @@ is observed to be mutated.
MutableMemoryView(::Unsafe, x::MemoryView{T}) where {T} =
MutableMemoryView{T}(unsafe, x.ref, x.len)

# 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}
end

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

"""
MemoryKind

Expand Down
6 changes: 6 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ end
s = Test.GenericString("abγδf")
@test codeunits(s) == MemoryView(s)
@test codeunits(s[2:(end - 2)]) == MemoryView(s)[2:(end - 1)]

x = [1,2,3]
@test MemoryView{Int}(x) isa MutableMemoryView{Int}
@test ImmutableMemoryView{Int}(x) isa ImmutableMemoryView{Int}
@test_throws TypeError MemoryView{UInt32}(x)
@test_throws TypeError ImmutableMemoryView{UInt32}(x)
end

@testset "Immutable views are immutable" begin
Expand Down
Loading