From b41913bbce3c392c9b78c44cf5fd17d36c9fa58c Mon Sep 17 00:00:00 2001 From: Kristoffer Date: Mon, 24 Apr 2023 17:17:48 +0200 Subject: [PATCH 1/2] use an extension for FixedPointNumbers --- Project.toml | 7 +++++++ ext/RatiosFixedPointNumbersExt.jl | 16 ++++++++++++++++ src/Ratios.jl | 17 ++++++++--------- 3 files changed, 31 insertions(+), 9 deletions(-) create mode 100644 ext/RatiosFixedPointNumbersExt.jl diff --git a/Project.toml b/Project.toml index 66019ab..d8ce719 100644 --- a/Project.toml +++ b/Project.toml @@ -6,10 +6,17 @@ version = "0.4.3" [deps] Requires = "ae029012-a4dd-5104-9daa-d747884805df" +[weakdeps] +FixedPointNumbers = "53c48c17-4a7d-5ca2-90c5-79b7896eea93" + [compat] +FixedPointNumbers = "0.8" Requires = "1" julia = "0.7, 1" +[extensions] +RatiosFixedPointNumbersExt = "FixedPointNumbers" + [extras] FixedPointNumbers = "53c48c17-4a7d-5ca2-90c5-79b7896eea93" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" diff --git a/ext/RatiosFixedPointNumbersExt.jl b/ext/RatiosFixedPointNumbersExt.jl new file mode 100644 index 0000000..6a333b8 --- /dev/null +++ b/ext/RatiosFixedPointNumbersExt.jl @@ -0,0 +1,16 @@ +module RatiosFixedPointNumbersExt + +using Ratios +isdefined(Base, :get_extension) ? (using FixedPointNumbers) : (using ..FixedPointNumbers) + +using .FixedPointNumbers: FixedPoint, Fixed, Normed, rawone + +rawone_noerr(::Type{Fixed{T,f}}) where {T,f} = widen(oneunit(T)) << f +rawone_noerr(::Type{N}) where N<:Normed = rawone(N) +rawone_noerr(x::FixedPoint) = rawone_noerr(typeof(x)) +Base.promote_rule(::Type{SimpleRatio{S}}, ::Type{<:FixedPoint{T}}) where {S<:Integer,T<:Integer} = SimpleRatio{promote_type(S, T)} +Ratios.SimpleRatio{S}(x::FixedPoint) where S<:Integer = SimpleRatio{S}(reinterpret(x), rawone_noerr(x)) +Ratios.SimpleRatio(x::FixedPoint) = SimpleRatio(reinterpret(x), rawone_noerr(x)) +Base.convert(::Type{S}, x::FixedPoint) where S<:SimpleRatio = S(x) + +end diff --git a/src/Ratios.jl b/src/Ratios.jl index 7566d25..719fcdc 100644 --- a/src/Ratios.jl +++ b/src/Ratios.jl @@ -12,7 +12,6 @@ module Ratios import Base: convert, promote_rule, *, /, +, -, ^, ==, decompose -using Requires export SimpleRatio, common_denominator @@ -121,17 +120,17 @@ function common_denominator(x::SimpleRatio, ys::SimpleRatio...) end end + +if !isdefined(Base, :get_extension) + using Requires +end + +@static if !isdefined(Base, :get_extension) function __init__() @require FixedPointNumbers = "53c48c17-4a7d-5ca2-90c5-79b7896eea93" begin - using .FixedPointNumbers: FixedPoint, Fixed, Normed, rawone - rawone_noerr(::Type{Fixed{T,f}}) where {T,f} = widen(oneunit(T)) << f - rawone_noerr(::Type{N}) where N<:Normed = rawone(N) - rawone_noerr(x::FixedPoint) = rawone_noerr(typeof(x)) - Base.promote_rule(::Type{SimpleRatio{S}}, ::Type{<:FixedPoint{T}}) where {S<:Integer,T<:Integer} = SimpleRatio{promote_type(S, T)} - SimpleRatio{S}(x::FixedPoint) where S<:Integer = SimpleRatio{S}(reinterpret(x), rawone_noerr(x)) - SimpleRatio(x::FixedPoint) = SimpleRatio(reinterpret(x), rawone_noerr(x)) - Base.convert(::Type{S}, x::FixedPoint) where S<:SimpleRatio = S(x) + include("../ext/RatiosFixedPointNumbersExt.jl") end end +end end From 10b24c6ee848fbb155961eedd5ff30af2dd63ed3 Mon Sep 17 00:00:00 2001 From: Kristoffer Carlsson Date: Mon, 24 Apr 2023 23:05:02 +0200 Subject: [PATCH 2/2] update compat --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index d8ce719..5c51834 100644 --- a/Project.toml +++ b/Project.toml @@ -10,7 +10,7 @@ Requires = "ae029012-a4dd-5104-9daa-d747884805df" FixedPointNumbers = "53c48c17-4a7d-5ca2-90c5-79b7896eea93" [compat] -FixedPointNumbers = "0.8" +FixedPointNumbers = "0.5, 0.6, 0.7, 0.8" Requires = "1" julia = "0.7, 1"