Skip to content

Commit 93a7908

Browse files
Merge pull request #443 from DhairyaLGandhi/dg/convert
chore: add `has_trivial_array_constructor`
2 parents 87b6460 + 193993c commit 93a7908

File tree

4 files changed

+35
-3
lines changed

4 files changed

+35
-3
lines changed

Project.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ BandedMatrices = "aae01518-5342-5314-be14-df237901396f"
5252
BlockBandedMatrices = "ffab5731-97b5-5995-9138-79e8c1846df0"
5353
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
5454
ChainRules = "082447d4-558c-5d27-93f4-14fc19e9eca2"
55+
ComponentArrays = "b0b7db55-cfe3-40fc-9ded-d10e2dbeff66"
5556
FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b"
5657
GPUArraysCore = "46192b85-c4d5-4398-a991-12ede77f4527"
5758
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
@@ -66,4 +67,4 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
6667
Tracker = "9f7883ad-71c0-57eb-9f7f-b5c9e6d3789c"
6768

6869
[targets]
69-
test = ["SafeTestsets", "Pkg", "Test", "Aqua", "Random", "SparseArrays", "SuiteSparse", "BandedMatrices", "BlockBandedMatrices", "GPUArraysCore", "StaticArrays", "StaticArraysCore", "Tracker", "ReverseDiff", "ChainRules", "FillArrays"]
70+
test = ["SafeTestsets", "Pkg", "Test", "Aqua", "Random", "SparseArrays", "SuiteSparse", "BandedMatrices", "BlockBandedMatrices", "GPUArraysCore", "StaticArrays", "StaticArraysCore", "Tracker", "ReverseDiff", "ChainRules", "FillArrays", "ComponentArrays"]

docs/src/conversions.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ ArrayInterface.aos_to_soa
1212
ArrayInterface.promote_eltype
1313
ArrayInterface.restructure
1414
ArrayInterface.safevec
15-
```
15+
ArrayInterface.has_trivial_array_constructor
16+
```

src/ArrayInterface.jl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,4 +1000,28 @@ ensures_sorted(@nospecialize( T::Type{<:AbstractRange})) = true
10001000
ensures_sorted(T::Type) = is_forwarding_wrapper(T) ? ensures_sorted(parent_type(T)) : false
10011001
ensures_sorted(@nospecialize(x)) = ensures_sorted(typeof(x))
10021002

1003+
"""
1004+
has_trivial_array_constructor(T::Type, args...) -> Bool
1005+
1006+
Returns `true` if an object of type `T` can be constructed using the collection of `args`
1007+
1008+
Note: This checks if a compatible `convert` methood exists between `T` and `args`
1009+
1010+
# Examples:
1011+
1012+
```julia
1013+
julia> ca = ComponentVector((x = rand(3), y = rand(4),))
1014+
ComponentVector{Float64}(x = [0.6549137106381634, 0.37555505280294565, 0.8521039568665254], y = [0.40314196291239024, 0.35484725607638834, 0.6580528978034597, 0.10055508457632167])
1015+
1016+
julia> ArrayInterface.has_trivial_array_constructor(typeof(ca), ones(6))
1017+
true
1018+
1019+
julia> ArrayInterface.has_trivial_array_constructor(typeof(cv), (x = rand(6),))
1020+
false
1021+
```
1022+
"""
1023+
function has_trivial_array_constructor(::Type{T}, args...) where T
1024+
applicable(convert, T, args...)
1025+
end
1026+
10031027
end # module

test/core.jl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ using ArrayInterface
22
using ArrayInterface: zeromatrix, undefmatrix
33
import ArrayInterface: has_sparsestruct, findstructralnz, fast_scalar_indexing, lu_instance,
44
parent_type, zeromatrix
5+
using ComponentArrays
56
using LinearAlgebra
67
using Random
78
using SparseArrays
@@ -289,4 +290,9 @@ end
289290
end
290291
@test ArrayInterface.ldlt_instance(SymTridiagonal(A' * A)) isa typeof(ldlt(SymTridiagonal(A' * A)))
291292
end
292-
end
293+
end
294+
295+
@testset "Array conversion" begin
296+
cv = ComponentVector((x = rand(3), y = rand(3)))
297+
@test ArrayInterface.has_trivial_array_constructor(typeof(cv), rand(6))
298+
end

0 commit comments

Comments
 (0)