diff --git a/.JuliaFormatter.toml b/.JuliaFormatter.toml new file mode 100644 index 0000000..3494a9f --- /dev/null +++ b/.JuliaFormatter.toml @@ -0,0 +1,3 @@ +style = "sciml" +format_markdown = true +format_docstrings = true diff --git a/README.md b/README.md index 2cd515d..778d0b2 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,4 @@ [![Build Status](https://github.com/chriselrod/FunctionWrappersWrappers.jl/actions/workflows/CI.yml/badge.svg?branch=main)](https://github.com/chriselrod/FunctionWrappersWrappers.jl/actions/workflows/CI.yml?query=branch%3Amain) [![Coverage](https://codecov.io/gh/chriselrod/FunctionWrappersWrappers.jl/branch/main/graph/badge.svg)](https://codecov.io/gh/chriselrod/FunctionWrappersWrappers.jl) - If wrapping your functions once makes them better, why not try wrapping them twice? - diff --git a/docs/make.jl b/docs/make.jl index 43f9db1..34eebfd 100644 --- a/docs/make.jl +++ b/docs/make.jl @@ -1,24 +1,25 @@ using FunctionWrappersWrappers using Documenter -DocMeta.setdocmeta!(FunctionWrappersWrappers, :DocTestSetup, :(using FunctionWrappersWrappers); recursive=true) +DocMeta.setdocmeta!(FunctionWrappersWrappers, :DocTestSetup, + :(using FunctionWrappersWrappers); recursive = true) makedocs(; - modules=[FunctionWrappersWrappers], - authors="Chris Elrod and contributors", - repo="https://github.com/chriselrod/FunctionWrappersWrappers.jl/blob/{commit}{path}#{line}", - sitename="FunctionWrappersWrappers.jl", - format=Documenter.HTML(; - prettyurls=get(ENV, "CI", "false") == "true", - canonical="https://chriselrod.github.io/FunctionWrappersWrappers.jl", - assets=String[], + modules = [FunctionWrappersWrappers], + authors = "Chris Elrod and contributors", + repo = "https://github.com/chriselrod/FunctionWrappersWrappers.jl/blob/{commit}{path}#{line}", + sitename = "FunctionWrappersWrappers.jl", + format = Documenter.HTML(; + prettyurls = get(ENV, "CI", "false") == "true", + canonical = "https://chriselrod.github.io/FunctionWrappersWrappers.jl", + assets = String[] ), - pages=[ - "Home" => "index.md", - ], + pages = [ + "Home" => "index.md" + ] ) deploydocs(; - repo="github.com/chriselrod/FunctionWrappersWrappers.jl", - devbranch="main", + repo = "github.com/chriselrod/FunctionWrappersWrappers.jl", + devbranch = "main" ) diff --git a/src/FunctionWrappersWrappers.jl b/src/FunctionWrappersWrappers.jl index 80a7279..7da5b9b 100644 --- a/src/FunctionWrappersWrappers.jl +++ b/src/FunctionWrappersWrappers.jl @@ -5,16 +5,24 @@ import TruncatedStacktraces export FunctionWrappersWrapper -struct FunctionWrappersWrapper{FW,FB} - fw::FW +struct FunctionWrappersWrapper{FW, FB} + fw::FW end TruncatedStacktraces.@truncate_stacktrace FunctionWrappersWrapper -(fww::FunctionWrappersWrapper{FW,FB})(args::Vararg{Any,K}) where {FW,K,FB} = _call(fww.fw, args, fww) +function (fww::FunctionWrappersWrapper{FW, FB})(args::Vararg{Any, K}) where {FW, K, FB} + _call(fww.fw, args, fww) +end -_call(fw::Tuple{FunctionWrappers.FunctionWrapper{R,A},Vararg}, arg::A, fww::FunctionWrappersWrapper) where {R,A} = first(fw)(arg...) -_call(fw::Tuple{FunctionWrappers.FunctionWrapper{R,A1},Vararg}, arg::A2, fww::FunctionWrappersWrapper) where {R,A1,A2} = _call(Base.tail(fw), arg, fww) +function _call(fw::Tuple{FunctionWrappers.FunctionWrapper{R, A}, Vararg}, + arg::A, fww::FunctionWrappersWrapper) where {R, A} + first(fw)(arg...) +end +function _call(fw::Tuple{FunctionWrappers.FunctionWrapper{R, A1}, Vararg}, + arg::A2, fww::FunctionWrappersWrapper) where {R, A1, A2} + _call(Base.tail(fw), arg, fww) +end const NO_FUNCTIONWRAPPER_FOUND_MESSAGE = "No matching function wrapper was found!" @@ -24,14 +32,20 @@ function Base.showerror(io::IO, e::NoFunctionWrapperFoundError) print(io, NO_FUNCTIONWRAPPER_FOUND_MESSAGE) end -_call(::Tuple{}, arg, fww::FunctionWrappersWrapper{<:Any,false}) = throw(NoFunctionWrapperFoundError()) -_call(::Tuple{}, arg, fww::FunctionWrappersWrapper{<:Any,true}) = first(fww.fw).obj[](arg...) +function _call(::Tuple{}, arg, fww::FunctionWrappersWrapper{<:Any, false}) + throw(NoFunctionWrapperFoundError()) +end +function _call(::Tuple{}, arg, fww::FunctionWrappersWrapper{<:Any, true}) + first(fww.fw).obj[](arg...) +end -function FunctionWrappersWrapper(f::F, argtypes::Tuple{Vararg{Any,K}}, rettypes::Tuple{Vararg{DataType,K}}, fallback::Val{FB}=Val{false}()) where {F,K,FB} - fwt = map(argtypes, rettypes) do A, R - FunctionWrappers.FunctionWrapper{R,A}(f) - end - FunctionWrappersWrapper{typeof(fwt),FB}(fwt) +function FunctionWrappersWrapper( + f::F, argtypes::Tuple{Vararg{Any, K}}, rettypes::Tuple{Vararg{DataType, K}}, + fallback::Val{FB} = Val{false}()) where {F, K, FB} + fwt = map(argtypes, rettypes) do A, R + FunctionWrappers.FunctionWrapper{R, A}(f) + end + FunctionWrappersWrapper{typeof(fwt), FB}(fwt) end end diff --git a/test/runtests.jl b/test/runtests.jl index 6a08fe9..64e29a2 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -2,14 +2,14 @@ using FunctionWrappersWrappers using Test @testset "FunctionWrappersWrappers.jl" begin - - fwplus = FunctionWrappersWrapper(+, (Tuple{Float64,Float64}, Tuple{Int,Int}), (Float64,Int)); - @test fwplus(4.0, 8.0) === 12.0 - @test fwplus(4, 8) === 12 - - fwexp2 = FunctionWrappersWrapper(exp2, (Tuple{Float64}, Tuple{Float32}, Tuple{Int}), (Float64,Float32,Float64)); - @test fwexp2(4.0) === 16.0 - @test fwexp2(4f0) === 16f0 - @test fwexp2(4) === 16.0 + fwplus = FunctionWrappersWrapper( + +, (Tuple{Float64, Float64}, Tuple{Int, Int}), (Float64, Int)) + @test fwplus(4.0, 8.0) === 12.0 + @test fwplus(4, 8) === 12 + fwexp2 = FunctionWrappersWrapper( + exp2, (Tuple{Float64}, Tuple{Float32}, Tuple{Int}), (Float64, Float32, Float64)) + @test fwexp2(4.0) === 16.0 + @test fwexp2(4.0f0) === 16.0f0 + @test fwexp2(4) === 16.0 end