Skip to content

Commit

Permalink
Merge pull request #27 from iamed2/ale/specializations
Browse files Browse the repository at this point in the history
 avoid specializing convert for Any result types (superseding #25)
  • Loading branch information
0x0f0f0f committed Jul 30, 2024
2 parents daf5da4 + 1fa5426 commit 89175a4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ResultTypes"
uuid = "08a2b407-ddc3-586a-afd6-c784ad1fffe2"
authors = ["Eric Davies"]
version = "3.1.0"
version = "3.2.0"

[compat]
julia = "1"
Expand Down
12 changes: 12 additions & 0 deletions src/ResultTypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,18 @@ function Base.convert(::Type{Result{S, E}}, r::Result) where {S, E <: Exception}
return promote_type(Result{S, E}, typeof(r))(r.result, r.error)
end

function Base.convert(::Type{Result{Any, E}}, r::Result) where {E <: Exception}
return promote_type(Result{Any, E}, typeof(r))(r.result, r.error)
end

function Base.convert(::Type{Result{S, E}}, x::T) where {T, S, E <: Exception}
return Result{S, E}(Some(convert(S, x)), nothing)
end

function Base.convert(::Type{Result{Any, E}}, @nospecialize(x)) where {E <: Exception}
return Result{Any, E}(Some{Any}(x), nothing)
end

function Base.convert(::Type{Result{T, E}}, e::E) where {T, E <: Exception}
return Result{T, E}(nothing, e)
end
Expand All @@ -125,6 +133,10 @@ function Base.convert(::Type{Result{T, E}}, e::E2) where {T, E <: Exception, E2
return Result{T,E}(nothing, convert(E, e))
end

function Base.convert(::Type{Result{Any, E}}, e::E2) where {E <: Exception, E2 <: Exception}
return Result{Any,E}(nothing, convert(E, e))
end

function Base.show(io::IO, r::Result{T, E}) where {T, E <: Exception}
if iserror(r)
print(io, "ErrorResult(", T, ", ", unwrap_error(r), ")")
Expand Down
12 changes: 12 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,18 @@ end

@test isempty(ambs)
end

@testset "Specializations" begin
AnyRes = Result{Any, Exception}
struct MyT end

convert(AnyRes, MyT())
method = only(methods(convert, Tuple{Type{AnyRes}, MyT}))
specs = method.specializations
specs_v = method.specializations isa Core.MethodInstance ? [specs] : collect(method.specializations)
@test only(filter(!isnothing, specs_v)).specTypes ==
Tuple{typeof(convert), Type{AnyRes}, Any}
end
end

@testset "Return" begin
Expand Down

2 comments on commit 89175a4

@0x0f0f0f
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register

Release notes:

Performance improvements: avoid specializations on any result types

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/112015

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v3.2.0 -m "<description of version>" 89175a4a11e598d7f52f4a31365f26e2664f649b
git push origin v3.2.0

Please sign in to comment.