Skip to content

Commit 28345bd

Browse files
authored
Merge pull request #8 from jw3126/pt
add StructTypes.jl support
2 parents 2e38117 + c90e886 commit 28345bd

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

Project.toml

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
name = "StructHelpers"
22
uuid = "4093c41a-2008-41fd-82b8-e3f9d02b504f"
33
authors = ["Jan Weidner <[email protected]> and contributors"]
4-
version = "1.1"
4+
version = "1.1.0"
55

66
[deps]
77
ConstructionBase = "187b0558-2788-49d3-abe0-74a17ed4e7c9"
88

99
[compat]
10-
julia = "1.6"
1110
ConstructionBase = "1.3"
11+
julia = "1.6"
1212

1313
[extras]
1414
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
15+
StructTypes = "856f2bd8-1eba-4b0a-8007-ebc267875bd4"
1516

1617
[targets]
17-
test = ["Test"]
18+
test = ["Test", "StructTypes"]

src/StructHelpers.jl

+11
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ const BATTERIES_DEFAULTS = (
8282
getproperties = true ,
8383
constructorof = true ,
8484
typesalt = nothing,
85+
StructTypes = false,
8586
)
8687

8788
const BATTERIES_DOCSTRINGS = (
@@ -94,6 +95,7 @@ const BATTERIES_DOCSTRINGS = (
9495
getproperties = "Overload `ConstructionBase.getproperties`.",
9596
constructorof = "Overload `ConstructionBase.constructorof`.",
9697
typesalt = "Only used if `hash=true`. In this case the `hash` will be purely computed from `typesalt` and `hash_eq_as(obj)`. The type `T` will not be used otherwise. This makes the hash more likely to stay constant, when executing on a different machine or julia version",
98+
StructTypes = "Overload `StructTypes.StructType` to be `Struct()`. Needs the `StructTypes.jl` package to be installed.",
9799
)
98100

99101
if (keys(BATTERIES_DEFAULTS) != keys(BATTERIES_DOCSTRINGS))
@@ -179,6 +181,11 @@ macro batteries(T, kw...)
179181
if need_fieldnames
180182
fieldnames = Base.fieldnames(Base.eval(__module__, T))
181183
end
184+
need_StructTypes = nt.StructTypes
185+
ST = gensym("ST")
186+
if need_StructTypes
187+
push!(ret.args, :(import StructTypes as $ST))
188+
end
182189
if nt.hash
183190
def = :(function Base.hash(o::$T, h::UInt)
184191
h = ($start_hash)(o, h, $(nt.typesalt))
@@ -222,6 +229,10 @@ macro batteries(T, kw...)
222229
def = def_selfconstructor(T)
223230
push!(ret.args, def)
224231
end
232+
if nt.StructTypes
233+
def = :($ST.StructType(::Type{<:$T}) = $ST.Struct())
234+
push!(ret.args, def)
235+
end
225236
return esc(ret)
226237
end
227238

test/runtests.jl

+13
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ struct Bad end
166166
@test_throws "`typesalt` must be literally `nothing` or an unsigned integer." @macroexpand @batteries Bad typesalt = "ouch"
167167
@test_throws "Unsupported keyword." @macroexpand @batteries Bad does_not_exist = true
168168
@test_throws "Bad keyword argument value" @macroexpand @batteries Bad hash=:nonsense
169+
@test_throws "Bad keyword argument value" @macroexpand @batteries Bad StructTypes=:nonsense
169170
end
170171
end
171172

@@ -235,3 +236,15 @@ Base.:(==)(::HashEqErr, ::HashEqErr) = error()
235236
@test SH.structural_hash(S(2,NaN), UInt(0)) == SH.structural_hash(S(2,NaN), UInt(0))
236237
@test SH.structural_hash(S(2,NaN), UInt(0)) != SH.structural_hash(S(2,NaN), UInt(1))
237238
end
239+
240+
struct WithStructTypes
241+
x
242+
y
243+
end
244+
SH.@batteries WithStructTypes StructTypes=true
245+
246+
import StructTypes as ST
247+
@testset "StructTypes" begin
248+
with = WithStructTypes(1,2)
249+
@test ST.StructType(typeof(with)) == ST.Struct()
250+
end

0 commit comments

Comments
 (0)