Skip to content

Commit a4bc86e

Browse files
author
Christopher Doris
committed
reparameterise PyArray
1 parent 333b9a3 commit a4bc86e

File tree

2 files changed

+132
-139
lines changed

2 files changed

+132
-139
lines changed

src/API/types.jl

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ baremodule pybuiltins end
5151
# Wrap
5252

5353
"""
54-
PyArray{T,N,M,L,R}(x; copy=true, array=true, buffer=true)
54+
PyArray{T,N,F}(x; copy=true, array=true, buffer=true)
5555
5656
Wrap the Python array `x` as a Julia `AbstractArray{T,N}`.
5757
@@ -62,31 +62,35 @@ If `copy=false` then the resulting array is guaranteed to directly wrap the data
6262
The type parameters are all optional, and are:
6363
- `T`: The element type.
6464
- `N`: The number of dimensions.
65-
- `M`: True if the array is mutable.
66-
- `L`: True if the array supports fast linear indexing.
67-
- `R`: The element type of the underlying buffer. Often equal to `T`.
68-
"""
69-
struct PyArray{T,N,M,L,R} <: AbstractArray{T,N}
70-
ptr::Ptr{R} # pointer to the data
65+
- `F`: Tuple of symbols, including:
66+
- `:mutable`: The array is mutable.
67+
- `:linear`: Supports fast linear indexing.
68+
- `:contiguous`: Data is F-contiguous. Implies `:linear`.
69+
- `:copy`/`:nocopy`: Whether or not a copy of the data may be taken.
70+
"""
71+
struct PyArray{T,N,F} <: AbstractArray{T,N}
72+
ptr::Ptr{Cvoid} # pointer to the data
7173
length::Int # length of the array
7274
size::NTuple{N,Int} # size of the array
7375
strides::NTuple{N,Int} # strides (in bytes) between elements
7476
py::Py # underlying python object
7577
handle::Py # the data in this array is valid as long as this handle is alive
76-
function PyArray{T,N,M,L,R}(
78+
function PyArray{T,N,F}(
7779
::Val{:new},
78-
ptr::Ptr{R},
80+
ptr::Ptr,
7981
size::NTuple{N,Int},
8082
strides::NTuple{N,Int},
8183
py::Py,
8284
handle::Py,
83-
) where {T,N,M,L,R}
85+
) where {T,N,F}
8486
T isa Type || error("T must be a Type")
8587
N isa Int || error("N must be an Int")
86-
M isa Bool || error("M must be a Bool")
87-
L isa Bool || error("L must be a Bool")
88-
R isa DataType || error("R must be a DataType")
89-
new{T,N,M,L,R}(ptr, prod(size), size, strides, py, handle)
88+
F isa Tuple{Vararg{Symbol}} || error("F must be a tuple of Symbols")
89+
for flag in F
90+
flag in (:mutable, :linear, :contiguous, :copy, :nocopy) ||
91+
error("invalid entry of F: $(repr(flag))")
92+
end
93+
new{T,N,F}(ptr, prod(size), size, strides, py, handle)
9094
end
9195
end
9296

0 commit comments

Comments
 (0)