Skip to content

Commit

Permalink
preserve staticarrays
Browse files Browse the repository at this point in the history
  • Loading branch information
aplavin committed Mar 31, 2023
1 parent 8cd189f commit da70ec2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
3 changes: 3 additions & 0 deletions ext/StaticArraysExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ import Accessors: setindex, delete, insert
@inline setindex(a::StaticArrays.StaticArray, args...) = Base.setindex(a, args...)
@inline delete(obj::StaticArrays.SVector, l::IndexLens) = StaticArrays.deleteat(obj, only(l.indices))
@inline insert(obj::StaticArrays.SVector, l::IndexLens, val) = StaticArrays.insert(obj, only(l.indices), val)

Accessors.set(obj::StaticArrays.SVector, ::Type{Tuple}, val::Tuple) = StaticArrays.SVector(val)

end
12 changes: 9 additions & 3 deletions test/test_functionlenses.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ using Unitful
using InverseFunctions: inverse
using Accessors: test_getset_laws, test_modify_law
using Accessors
using StaticArrays: SVector


@testset "os" begin
path = "hello.md"
Expand Down Expand Up @@ -81,14 +83,16 @@ end
@test @modify(m -> m + 1, x |> convert(Minute, _).value) === Second(240)
@test_throws ArgumentError @set x |> convert(Minute, _) = 123
test_getset_laws(@optic(convert(Minute, _)), x, Minute(10), Minute(20))

cmp(a::NamedTuple, b::NamedTuple) = Set(keys(a)) == Set(keys(b)) && NamedTuple{keys(b)}(a) === b
cmp(a::T, b::T) where {T} = a == b

test_getset_laws(Tuple, (1, 'a'), ('x', 'y'), (1, 2))
test_getset_laws(Tuple, (a=1, b='a'), ('x', 'y'), (1, 2))
test_getset_laws(Tuple, [0, 1], ('x', 'y'), (1, 2))
test_getset_laws(Tuple, [0, 1], ('x', 'y'), (1, 2); cmp=cmp)
test_getset_laws(Tuple, SVector(0, 1), ('x', 'y'), (1, 2); cmp=cmp)
test_getset_laws(Tuple, CartesianIndex(1, 2), (3, 4), (5, 6))

cmp(a::NamedTuple, b::NamedTuple) = Set(keys(a)) == Set(keys(b)) && NamedTuple{keys(b)}(a) === b
cmp(a::T, b::T) where {T} = a == b
test_getset_laws(NamedTuple{(:x, :y)}, (1, 'a'), (x='x', y='y'), (x=1, y=2); cmp=cmp)
test_getset_laws(NamedTuple{(:x, :y)}, (1, 'a'), (y='x', x='y'), (x=1, y=2); cmp=cmp)
test_getset_laws(NamedTuple{(:x, :y)}, (y=1, x='a'), (x='x', y='y'), (x=1, y=2); cmp=cmp)
Expand All @@ -97,6 +101,8 @@ end
test_getset_laws(NamedTuple{(:x, :y)}, (y=1, z=10, x='a'), (y='x', x='y'), (x=1, y=2); cmp=cmp)
test_getset_laws(NamedTuple{(:x, :y)}, [0, 1], (x='x', y='y'), (x=1, y=2); cmp=cmp)
test_getset_laws(NamedTuple{(:x, :y)}, [0, 1], (y='x', x='y'), (x=1, y=2); cmp=cmp)
test_getset_laws(NamedTuple{(:x, :y)}, SVector(0, 1), (x='x', y='y'), (x=1, y=2); cmp=cmp)
test_getset_laws(NamedTuple{(:x, :y)}, SVector(0, 1), (y='x', x='y'), (x=1, y=2); cmp=cmp)
test_getset_laws(NamedTuple{(:x, :y)}, CartesianIndex(1, 2), (x=3, y=4), (x=5, y=6); cmp=cmp)
test_getset_laws(NamedTuple{(:x, :y)}, CartesianIndex(1, 2), (y=3, x=4), (x=5, y=6); cmp=cmp)
end
Expand Down

0 comments on commit da70ec2

Please sign in to comment.