Skip to content

Commit e0d387b

Browse files
committed
speedup enum conversion
1 parent ec2fd64 commit e0d387b

File tree

1 file changed

+39
-4
lines changed

1 file changed

+39
-4
lines changed

src/StructHelpers.jl

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -315,22 +315,29 @@ end
315315
function enum_from_string end
316316
function enum_from_symbol end
317317
function string_from_enum(x)::String
318+
string_from_enum_generic(x)
319+
end
320+
function string_from_enum_generic(x)::String
318321
string(x)
319322
end
323+
320324
function symbol_from_enum(x)::Symbol
321-
Symbol(string_from_enum(x))
325+
symbol_from_enum_generic(x)
326+
end
327+
function symbol_from_enum_generic(x)::Symbol
328+
Symbol(string_from_enum_generic(x))
322329
end
323330

324331
function def_enum_from_string(T)::Expr
325-
body = def_symbol_or_enum_from_string_body(string_from_enum, T)
332+
body = def_enum_from_string_or_symbol_body(string_from_enum, T)
326333
:(
327334
function $SH.enum_from_string(::Type{$T}, s::String)::$T
328335
$body
329336
end
330337
)
331338
end
332339
function def_enum_from_symbol(T)::Expr
333-
body = def_symbol_or_enum_from_string_body(QuoteNodesymbol_from_enum, T)
340+
body = def_enum_from_string_or_symbol_body(QuoteNodesymbol_from_enum, T)
334341
:(
335342
function $SH.enum_from_symbol(::Type{$T}, s::Symbol)::$T
336343
$body
@@ -348,7 +355,33 @@ end
348355
throw(ArgumentError(msg))
349356
end
350357

351-
function def_symbol_or_enum_from_string_body(f,T)
358+
function def_symbol_or_string_from_enum_body(s_from_enum_generic, T)::Expr
359+
default = :($s_from_enum_generic(obj))
360+
matcharms = [
361+
:(obj === $inst) => QuoteNode(s_from_enum_generic(inst)) for inst in instances(T)
362+
]
363+
ifelsechain(matcharms, default)
364+
end
365+
366+
function def_symbol_from_enum(T)::Expr
367+
body = def_symbol_or_string_from_enum_body(symbol_from_enum_generic, T)
368+
:(
369+
function $SH.symbol_from_enum(obj::$T)::Symbol
370+
$body
371+
end
372+
)
373+
end
374+
375+
function def_string_from_enum(T)::Expr
376+
body = def_symbol_or_string_from_enum_body(string_from_enum_generic, T)
377+
:(
378+
function $SH.string_from_enum(obj::$T)::String
379+
$body
380+
end
381+
)
382+
end
383+
384+
function def_enum_from_string_or_symbol_body(f,T)
352385
err = :($throw_no_matching_instance($f,$T,s))
353386
matcharms = [
354387
:(s === $(f(inst))) => inst for inst in instances(T)
@@ -440,6 +473,8 @@ macro enumbatteries(T, kw...)
440473
push!(ret.args, :(import StructHelpers as $SH))
441474
push!(ret.args, def_enum_from_symbol(TT))
442475
push!(ret.args, def_enum_from_string(TT))
476+
push!(ret.args, def_symbol_from_enum(TT))
477+
push!(ret.args, def_string_from_enum(TT))
443478
if nt.string_conversion
444479
ex1 = :(Base.convert(::Type{$TT}, s::AbstractString) = $SH.enum_from_string($TT, String(s)))
445480
ex2 = :($T(s::AbstractString) = $SH.enum_from_string($TT, String(s)))

0 commit comments

Comments
 (0)