Skip to content

Commit 90455b9

Browse files
committed
no mutation test + inline _modify
1 parent 926442a commit 90455b9

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

src/optics.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ function _modify(f, obj, optic::ComposedOptic, ::ModifyBased)
160160
end
161161
end
162162

163-
function _modify(f, obj, optic, ::SetBased)
163+
@inline function _modify(f, obj, optic, ::SetBased)
164164
set(obj, optic, f(optic(obj)))
165165
end
166166

test/test_setindex.jl

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@ Check that _type_ and value of `x` and `y` are equal.
1616
end
1717

1818
function ref_alloc_test()
19-
ref = Ref((; a = 10, b = 0x0001, c = (; aa = 300)))
20-
ref2 = Accessors.setindex(ref, (; a = 10, b = ref[].b - 1, c = (; aa = 300)))
21-
ref3 = Accessors.setindex(ref, ref2[])
19+
ref = Ref((UInt(10), 100))
20+
ref2 = @set ref[][1] *= -1
2221

23-
ref3[]
22+
ref2[]
2423
end
2524

2625
@testset "setindex" begin
@@ -41,14 +40,20 @@ end
4140
@test Accessors.setindex(d, 30, "c") ==Dict(:a=>1, :b=>2, "c"=>30)
4241
@test Accessors.setindex(d, 10.0, :a) ==Dict(:a=>10.0, :b=>2.0)
4342

44-
ref = Ref((; a = 10, b = 0x0001, c = (; aa = 300)))
45-
@test @set(ref[].a = 90)[] == Ref((; a = 90, b = 0x0001, c = (; aa = 300)))[]
46-
@test @set(ref[].b = Bool(ref[].b))[] == Ref((; a = 10, b = true, c = (; aa = 300)))[]
47-
@test @set(ref[].c.aa = 3)[] == Ref((; a = 10, b = 0x0001, c = (; aa = 3)))[]
48-
49-
local val
50-
@test @allocated(val = ref_alloc_test()) == 0
51-
@test val == (; a = 10, b = 0x0000, c = (; aa = 300))
43+
ref = Ref((; a = 1, b = 2, c = (; aa = 3)))
44+
@test @set(ref[].a = 90)[] == (; a = 90, b = 2, c = (; aa = 3))
45+
@test @set(ref[].b = "2")[] ==ₜ (; a = 1, b = "2", c = (; aa = 3))
46+
@test @set(ref[].c.aa += 2)[] == (; a = 1, b = 2, c = (; aa = 5))
47+
48+
ref = Ref(1::Int)
49+
@set ref[] = "no mutation"
50+
@test ref[] === 1
51+
@test typeof(ref) == Base.RefValue{Int}
52+
53+
if VERSION >= v"1.5.0"
54+
_ = ref_alloc_test()
55+
@test @allocated(ref_alloc_test()) == 0
56+
end
5257
end
5358

5459
end

0 commit comments

Comments
 (0)