Skip to content

Commit f1e33d4

Browse files
authored
Add function unsafe_from_parts (#25)
1 parent e5ec476 commit f1e33d4

4 files changed

Lines changed: 46 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ Any new features, or breaking changes, will be written in this file.
44
Bugfixes, internal refactors, documentation improvements and style changes will
55
not be mentioned here, because they do not impact how the package is to be used.
66

7+
## 0.3.4
8+
* Add new function `unsafe_from_parts` to construct a memory view from a
9+
`MemoryRef`.
10+
711
## 0.3.3
812
### New features
913
* Add a new `split_each` function, which iterates over memory views delimited

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "MemoryViews"
22
uuid = "a791c907-b98b-4e44-8f4d-e4c2362c6b2f"
3-
version = "0.3.3"
3+
version = "0.3.4"
44
authors = ["Jakob Nybo Nissen <jakobnybonissen@gmail.com>"]
55

66
[weakdeps]

src/MemoryViews.jl

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ export MemoryView,
77
IsMemory,
88
NotMemory,
99
inner,
10-
split_each
10+
split_each,
11+
unsafe_from_parts
1112

1213
public Mutable, Immutable, DelimitedIterator
1314

@@ -96,6 +97,35 @@ end
9697
const MutableMemoryView{T} = MemoryView{T, Mutable}
9798
const ImmutableMemoryView{T} = MemoryView{T, Immutable}
9899

100+
"""
101+
unsafe_from_parts(ref::MemoryRef{T}, len::Int)::MutableMemoryView{T}
102+
103+
Create a mutable memory view from its parts.
104+
105+
**Safety:** Callers are responsible to ensure that:
106+
* `len` is not negative
107+
* All indices `i in 1:len` are valid for `ref` (i.e. `memoryref(ref, i)` would
108+
not throw)
109+
* If `ref` is derived from immutable memory, the returned memory view must not
110+
be mutated. The caller should immediately convert it to an immutable view.
111+
112+
# Examples
113+
```jldoctest
114+
julia> v = [1,2,3,4];
115+
116+
julia> ref = Base.cconvert(Ptr, v);
117+
118+
julia> view = unsafe_from_parts(ref, 3)
119+
3-element MutableMemoryView{Int64}:
120+
1
121+
2
122+
3
123+
```
124+
"""
125+
function unsafe_from_parts(ref::MemoryRef{T}, len::Int) where {T}
126+
return MemoryView{T, Mutable}(unsafe, ref, len)
127+
end
128+
99129
_get_mutability(::MemoryView{T, M}) where {T, M} = M
100130

101131
# Mutable mem views can turn into immutable ones, but not vice versa

test/runtests.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,16 @@ end
693693
end
694694
end
695695

696+
@testset "From parts" begin
697+
v = [1 2; 3 4]
698+
ref = Base.cconvert(Ptr, v)
699+
mem = unsafe_from_parts(ref, 3)
700+
@test mem == [1, 3, 2]
701+
@test unsafe_from_parts(ref, 2) == [1, 3]
702+
@test isempty(unsafe_from_parts(ref, 0))
703+
@test mem isa MutableMemoryView{Int}
704+
end
705+
696706
@testset "MemoryKind" begin
697707
@test MemoryKind(Vector{Int16}) == IsMemory(MutableMemoryView{Int16})
698708
@test MemoryKind(typeof(codeunits(view("abc", 2:3)))) ==

0 commit comments

Comments
 (0)