Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "StructHelpers"
uuid = "4093c41a-2008-41fd-82b8-e3f9d02b504f"
authors = ["Jan Weidner <jw3126@gmail.com> and contributors"]
version = "1.3.1"
version = "1.4.0"

[deps]
ConstructionBase = "187b0558-2788-49d3-abe0-74a17ed4e7c9"
Expand Down
23 changes: 19 additions & 4 deletions src/StructHelpers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ end
has_batteries(T::Type)::Bool

Check if `@batteries` or `@enumbatteries` was applied to `T`.
See also [`batteries_options`](@ref).
"""
function has_batteries(::Type)::Bool
false
Expand Down Expand Up @@ -238,15 +239,29 @@ macro batteries(T, kw...)
def = :($ST.StructType(::Type{<:$T}) = $ST.Struct())
push!(ret.args, def)
end
push!(ret.args, def_has_batteries(T))
append!(ret.args, defs_has_batteries(T, nt))
return esc(ret)
end

function def_has_batteries(T)
:(
"""

batteries_options(::Type{T})::NamedTuple

Return the options for `@batteries` for type `T`. Assumes that `has_batteries(T) == true` holds.
"""
function batteries_options end

function defs_has_batteries(T, options)
(
:(
function $(SH).has_batteries(::Type{<:$T})::Bool
true
end),
:(
function $(SH).batteries_options(::Type{<:$T})::$(typeof(options))
return $options
end
)
)
end

Expand Down Expand Up @@ -466,7 +481,7 @@ macro enumbatteries(T, kw...)
)
push!(ret.args, def)
end
push!(ret.args, def_has_batteries(T))
append!(ret.args, defs_has_batteries(T, nt))
return esc(ret)
end

Expand Down
7 changes: 7 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ struct SNoIsEqual; a; end


@testset "typesalt" begin
@test SH.batteries_options(Salt1).typesalt == 1
@test SH.batteries_options(Salt1b).typesalt == 1
@test SH.batteries_options(Salt2).typesalt == 2
@test SH.batteries_options(NoSalt).typesalt === nothing
@test hash(Salt1()) === hash(Salt1b())
@test hash(Salt1()) != hash(NoSalt())
@test hash(Salt1()) != hash(Salt2())
Expand All @@ -121,6 +125,8 @@ struct SNoIsEqual; a; end
end

@test WithSelfCtor(WithSelfCtor(1)) === WithSelfCtor(1)
@test SH.batteries_options(WithSelfCtor).selfconstructor === true
@test SH.batteries_options(NoSelfCtor).selfconstructor === false
@test NoSelfCtor(NoSelfCtor(1)) != NoSelfCtor(1)
@test NoSelfCtor(NoSelfCtor(1)) isa NoSelfCtor
@test NoSelfCtor(NoSelfCtor(1)).a === NoSelfCtor(1)
Expand Down Expand Up @@ -192,6 +198,7 @@ end

h = 0xed315b93bf264f3e
typesalt = 0xd11b6121f2b8cd22
@test SH.batteries_options(Negative).typesalt === typesalt
@test hash(MinusOne, h) == hash(-1, hash(typesalt, h))
@test hash(MinusTwo, h) == hash(-2, hash(typesalt, h))
end
Expand Down
Loading