Skip to content

Commit 178cfb5

Browse files
authored
Add to_vec(CartesianIndex) (#197)
1 parent 2e3dd78 commit 178cfb5

File tree

3 files changed

+16
-13
lines changed

3 files changed

+16
-13
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "FiniteDifferences"
22
uuid = "26cc04aa-876d-5657-8c51-4c34ba976000"
3-
version = "0.12.19"
3+
version = "0.12.20"
44

55
[deps]
66
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"

src/to_vec.jl

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -235,12 +235,15 @@ function to_vec(d::Dict)
235235
return d_vec, Dict_from_vec
236236
end
237237

238-
# types
239-
function FiniteDifferences.to_vec(x::DataType)
240-
function DataType_from_vec(x_vec::Vector)
241-
return x
238+
# non-perturbable types
239+
for T in (:DataType, :CartesianIndex, :AbstractZero)
240+
T_from_vec = Symbol(T, :_from_vec)
241+
@eval function FiniteDifferences.to_vec(x::$T)
242+
function $T_from_vec(x_vec::Vector)
243+
return x
244+
end
245+
return Bool[], $T_from_vec
242246
end
243-
return Bool[], DataType_from_vec
244247
end
245248

246249
# ChainRulesCore Differentials
@@ -255,13 +258,6 @@ function FiniteDifferences.to_vec(x::Tangent{P}) where{P}
255258
return x_vec, Tangent_from_vec
256259
end
257260

258-
function FiniteDifferences.to_vec(x::AbstractZero)
259-
function AbstractZero_from_vec(x_vec::Vector)
260-
return x
261-
end
262-
return Bool[], AbstractZero_from_vec
263-
end
264-
265261
function FiniteDifferences.to_vec(t::Thunk)
266262
v, back = to_vec(unthunk(t))
267263
Thunk_from_vec = v -> @thunk(back(v))

test/to_vec.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,13 @@ end
200200
test_to_vec(Vector; check_inferred=false) # isa UnionAll
201201
end
202202

203+
@testset "CartesianIndex" begin
204+
test_to_vec(CartesianIndex(1))
205+
test_to_vec(CartesianIndex(1, 2))
206+
@test to_vec(CartesianIndex(1))[1] == []
207+
@test to_vec(CartesianIndex(1, 3))[1] == []
208+
end
209+
203210
@testset "ChainRulesCore Differentials" begin
204211
@testset "Tangent{Tuple}" begin
205212
@testset "basic" begin

0 commit comments

Comments
 (0)