From 26287419cbab3402934ebb410c88c3f361961643 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 30 Jul 2024 14:09:18 +0530 Subject: [PATCH] feat: support serialization of clocks --- src/clock.jl | 12 +++++++++++- test/runtests.jl | 3 +++ test/serialization_tests.jl | 18 ++++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 test/serialization_tests.jl diff --git a/src/clock.jl b/src/clock.jl index 04d5dac43..2db74dd3a 100644 --- a/src/clock.jl +++ b/src/clock.jl @@ -2,7 +2,7 @@ module Clocks export TimeDomain -using Expronicon.ADT: @adt, @match +using Expronicon.ADT: variant_type, @adt, @match @adt TimeDomain begin Continuous @@ -15,6 +15,16 @@ end Base.Broadcast.broadcastable(d::TimeDomain) = Ref(d) +const DiscriminatorType = typeof(variant_type(Continuous)) + +function Base.write(io::IO, x::DiscriminatorType) + write(io, Base.reinterpret(UInt32, x)) +end + +function Base.read(io::IO, ::Type{DiscriminatorType}) + Base.reinterpret(DiscriminatorType, read(io, UInt32)) +end + end using .Clocks diff --git a/test/runtests.jl b/test/runtests.jl index d1dddaea8..bf4b0d77f 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -60,6 +60,9 @@ end @time @safetestset "Problem building tests" begin include("problem_building_test.jl") end + @time @safetestset "Serialization tests" begin + include("serialization_tests.jl") + end end if !is_APPVEYOR && diff --git a/test/serialization_tests.jl b/test/serialization_tests.jl new file mode 100644 index 000000000..bcb2bf0c8 --- /dev/null +++ b/test/serialization_tests.jl @@ -0,0 +1,18 @@ +using SciMLBase +using Serialization +using Test + +for clock in [ + SciMLBase.Clock(0.5), + SciMLBase.Clock(0.5; phase = 0.1), + SciMLBase.SolverStepClock, + SciMLBase.Continuous, + ] + serialize("_tmp.jls", clock) + newclock = deserialize("_tmp.jls") + @test newclock == clock +end + +if isfile("_tmp.jls") + rm("_tmp.jls") +end