Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .JuliaFormatter.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
style = "sciml"
format_markdown = true
format_docstrings = true
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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?

29 changes: 15 additions & 14 deletions docs/make.jl
Original file line number Diff line number Diff line change
@@ -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 <elrodc@gmail.com> 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 <elrodc@gmail.com> 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"
)
38 changes: 26 additions & 12 deletions src/FunctionWrappersWrappers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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!"

Expand All @@ -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
18 changes: 9 additions & 9 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading