Skip to content

Commit d0ed2f2

Browse files
authored
add Future.copy! function (#25144)
1 parent 4d166fa commit d0ed2f2

File tree

5 files changed

+90
-1
lines changed

5 files changed

+90
-1
lines changed

base/deprecated.jl

+21
Original file line numberDiff line numberDiff line change
@@ -3587,9 +3587,30 @@ info(err::Exception; prefix="ERROR: ", kw...) =
35873587

35883588
# #24844
35893589
@deprecate copy!(dest::AbstractSet, src) union!(dest, src)
3590+
3591+
function copy!(dest::AbstractSet, src::AbstractSet)
3592+
depwarn("copy!(dst::AbstractSet, src::AbstractSet) is deprecated. " *
3593+
"You can either use union!(dst, src) of Future.copy!(dst, src) instead.", :copy!)
3594+
union!(dest, src)
3595+
end
3596+
35903597
@deprecate copy!(dest::AbstractDict, src) foldl(push!, dest, src)
3598+
3599+
function copy!(dest::AbstractDict, src::AbstractDict)
3600+
depwarn("copy!(dst::AbstractDict, src::AbstractDict) is deprecated. " *
3601+
"You can either use merge!(dst, src) of Future.copy!(dst, src) instead.", :copy!)
3602+
foldl(push!, dest, src)
3603+
end
3604+
35913605
# 24808
35923606
@deprecate copy!(dest::Union{AbstractArray,IndexStyle}, args...) copyto!(dest, args...)
3607+
3608+
function copy!(dest::AbstractArray, src::AbstractArray)
3609+
depwarn("copy!(dst::AbstractArray, src::AbstractArray) is deprecated. " *
3610+
"You can either use copyto!(dst, src) of Future.copy!(dst, src) instead.", :copy!)
3611+
copyto!(dest, src)
3612+
end
3613+
35933614
@deprecate unsafe_copy!(dest, args...) unsafe_copyto!(dest, args...)
35943615

35953616
# issue #24019

base/sysimg.jl

+1
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,7 @@ Base.require(:Test)
503503
Base.require(:Unicode)
504504
Base.require(:Distributed)
505505
Base.require(:Printf)
506+
Base.require(:Future)
506507

507508
@eval Base begin
508509
@deprecate_binding Test root_module(:Test) true ", run `using Test` instead"

stdlib/Future/src/Future.jl

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# This file is a part of Julia. License is MIT: https://julialang.org/license
2+
3+
"The `Future` module implements future behavior of already existing functions,
4+
which will replace the current version in a future release of Julia."
5+
module Future
6+
7+
## copy!
8+
9+
"""
10+
Future.copy!(dst, src) -> dst
11+
12+
Copy `src` into `dst`.
13+
For collections of the same type, copy the elements of `src` into `dst`,
14+
discarding any pre-existing elements in `dst`.
15+
Usually, `dst == src` holds after the call.
16+
"""
17+
copy!(dst::AbstractSet, src::AbstractSet) = union!(empty!(dst), src)
18+
copy!(dst::AbstractDict, src::AbstractDict) = merge!(empty!(dst), src)
19+
copy!(dst::AbstractVector, src::AbstractVector) = append!(empty!(dst), src)
20+
21+
function copy!(dst::AbstractArray, src::AbstractArray)
22+
size(dst) == size(src) || throw(ArgumentError(
23+
"arrays must have the same size for copy! (consider using `copyto!`)"))
24+
copyto!(dst, src)
25+
end
26+
27+
end # module Future

stdlib/Future/test/runtests.jl

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# This file is a part of Julia. License is MIT: https://julialang.org/license
2+
3+
using Test
4+
using Future
5+
6+
@testset "Future.copy! for AbstractSet" begin
7+
for S = (Set, BitSet)
8+
s = S([1, 2])
9+
for a = ([1], UInt[1], [3, 4, 5], UInt[3, 4, 5])
10+
@test s === Future.copy!(s, Set(a)) == S(a)
11+
@test s === Future.copy!(s, BitSet(a)) == S(a)
12+
end
13+
end
14+
end
15+
16+
17+
@testset "Future.copy! for AbstractDict" begin
18+
s = Dict(1=>2, 2=>3)
19+
for a = ([3=>4], [0x3=>0x4], [3=>4, 5=>6, 7=>8], Pair{UInt,UInt}[3=>4, 5=>6, 7=>8])
20+
@test s === Future.copy!(s, Dict(a)) == Dict(a)
21+
if length(a) == 1 # current limitation of Base.ImmutableDict
22+
@test s === Future.copy!(s, Base.ImmutableDict(a[])) == Dict(a[])
23+
end
24+
end
25+
end
26+
27+
@testset "Future.copy! for AbstractVector" begin
28+
s = Vector([1, 2])
29+
for a = ([1], UInt[1], [3, 4, 5], UInt[3, 4, 5])
30+
@test s === Future.copy!(s, Vector(a)) == Vector(a)
31+
@test s === Future.copy!(s, SparseVector(a)) == Vector(a)
32+
end
33+
end
34+
35+
@testset "Future.copy! for AbstractArray" begin
36+
@test_throws ArgumentError Future.copy!(zeros(2, 3), zeros(3, 2))
37+
s = zeros(2, 2)
38+
@test s === Future.copy!(s, fill(1, 2, 2)) == fill(1, 2, 2)
39+
@test s === Future.copy!(s, fill(1.0, 2, 2)) == fill(1.0, 2, 2)
40+
end

test/compile.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ try
218218
[:Base, :Core, Foo2_module, FooBase_module, :Main]),
219219
# plus modules included in the system image
220220
Dict(s => Base.module_uuid(Base.root_module(s)) for s in
221-
[:Base64, :CRC32c, :Dates, :DelimitedFiles, :FileWatching,
221+
[:Base64, :CRC32c, :Dates, :DelimitedFiles, :FileWatching, :Future,
222222
:IterativeEigensolvers, :Logging, :Mmap, :Printf, :Profile, :SharedArrays,
223223
:SuiteSparse, :Test, :Unicode, :Distributed]))
224224
@test discard_module.(deps) == deps1

0 commit comments

Comments
 (0)