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

Allow unsafe_wrapping PointerWrappers to vectors #82

Merged
merged 3 commits into from
May 8, 2023
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
2 changes: 1 addition & 1 deletion src/pointerwrappers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ Base.unsafe_load(pw::PointerWrapper, i::Integer=1) = unsafe_load(pointer(pw), i)

# When `unsafe_wrap`ping a PointerWrapper object, we really want to wrap the underlying array
Base.unsafe_wrap(AType::Union{Type{Array},Type{Array{T}},Type{Array{T,N}}},
pw::PointerWrapper, dims::NTuple{N,Int}; own::Bool = false) where {T,N} = unsafe_wrap(AType, pointer(pw), dims; own)
pw::PointerWrapper, dims::Union{NTuple{N,Int}, Integer}; own::Bool = false) where {T,N} = unsafe_wrap(AType, pointer(pw), dims; own)

# If value is of the wrong type, try to convert it
Base.unsafe_store!(pw::PointerWrapper{T}, value, i::Integer=1) where T = unsafe_store!(pw, convert(T, value), i)
Expand Down
20 changes: 13 additions & 7 deletions test/tests_basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,23 @@ end

# `unsafe_wrap`ping a `PointerWrapper`
n_vertices::Int = connectivity_pw.num_vertices[]
vertices = @test_nowarn unsafe_wrap(Array, connectivity_pw.vertices, (3, n_vertices))
@test vertices isa Array{Float64, 2}
# wrapping matrices
vertices_matrix = @test_nowarn unsafe_wrap(Array, connectivity_pw.vertices, (3, n_vertices))
@test vertices_matrix isa Array{Float64, 2}
@test unsafe_wrap(Array{Float64}, connectivity_pw.vertices, (3, n_vertices)) isa Array{Float64, 2}
@test unsafe_wrap(Array{Float64, 2}, connectivity_pw.vertices, (3, n_vertices)) isa Array{Float64, 2}

@test size(vertices) == (3, n_vertices)
@test vertices[1, 1] == connectivity_pw.vertices[1] == 0.0
@test_nowarn vertices[1, 1] = 1.0
@test vertices[1, 1] == connectivity_pw.vertices[1] == 1.0
@test size(vertices_matrix) == (3, n_vertices)
@test vertices_matrix[1, 1] == connectivity_pw.vertices[1] == 0.0
@test_nowarn vertices_matrix[1, 1] = 1.0
@test vertices_matrix[1, 1] == connectivity_pw.vertices[1] == 1.0
@test_nowarn connectivity_pw.vertices[1] = 2.0
@test vertices[1, 1] == connectivity_pw.vertices[1] == 2.0
@test vertices_matrix[1, 1] == connectivity_pw.vertices[1] == 2.0
# wrapping vectors
vertices_vector = @test_nowarn unsafe_wrap(Array, connectivity_pw.vertices, 3 * n_vertices)
@test vertices_vector isa Array{Float64, 1}
@test unsafe_wrap(Array{Float64}, connectivity_pw.vertices, 3 * n_vertices) isa Array{Float64, 1}
@test unsafe_wrap(Array{Float64, 1}, connectivity_pw.vertices, 3 * n_vertices) isa Array{Float64, 1}

@test_nowarn p4est_destroy(p4est_pw)
@test_nowarn p4est_connectivity_destroy(connectivity_pw)
Expand Down