Skip to content

Commit f73e73b

Browse files
committed
Make similar return a mutable view always
1 parent 0d9b2c6 commit f73e73b

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

src/basic.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,11 @@ function Base.getindex(v::MemoryView, i::Integer)
4444
end
4545

4646
function Base.similar(::MemoryView{T1, M}, ::Type{T2}, dims::Tuple{Int}) where {T1, T2, M}
47-
len = Int(only(dims))::Int
47+
len = only(dims)
4848
memory = Memory{T2}(undef, len)
49-
MemoryView{T2, M}(unsafe, memoryref(memory), len)
49+
# Note: `similar` needs to construct a mutable memory view, even if the input
50+
# type is not mutable
51+
MemoryView(memory)
5052
end
5153

5254
function Base.empty(::MemoryView{T1, M}, ::Type{T2}) where {T1, T2, M}
@@ -326,6 +328,7 @@ function Base.reverse!(mem::MutableMemoryView)
326328
mem
327329
end
328330

331+
# TODO: No need to copy
329332
function Base.reverse(mem::MemoryView)
330333
cp = MutableMemoryView(unsafe, copy(mem))
331334
stop = length(cp) + 1

test/runtests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ end
327327
mem = MemoryView("abc")
328328
mem2 = similar(mem)
329329
@test length(mem2) == length(mem)
330-
@test typeof(mem2) == typeof(mem)
330+
@test typeof(mem2) == MutableMemoryView{UInt8}
331331

332332
mem = MemoryView(String["", "", ""])
333333
mem2 = similar(mem, Int, 4)

0 commit comments

Comments
 (0)