Skip to content

Commit a007e80

Browse files
KristofferCKristofferC
andauthored
avoid defining convert(Vector{String}, ...) in LibGit2 (#56082)
This is a weird conversion function to define. Seems cleaner to use the iteration interface for this. Also avoids some invalidations (#56080 (comment)) Co-authored-by: KristofferC <[email protected]>
1 parent dc609a7 commit a007e80

File tree

7 files changed

+11
-10
lines changed

7 files changed

+11
-10
lines changed

stdlib/LibGit2/src/reference.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ function ref_list(repo::GitRepo)
215215
sa_ref = Ref(StrArrayStruct())
216216
@check ccall((:git_reference_list, libgit2), Cint,
217217
(Ptr{StrArrayStruct}, Ptr{Cvoid}), sa_ref, repo)
218-
res = convert(Vector{String}, sa_ref[])
218+
res = collect(sa_ref[])
219219
free(sa_ref)
220220
res
221221
end

stdlib/LibGit2/src/remote.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ function fetch_refspecs(rmt::GitRemote)
215215
sa_ref = Ref(StrArrayStruct())
216216
@check ccall((:git_remote_get_fetch_refspecs, libgit2), Cint,
217217
(Ptr{StrArrayStruct}, Ptr{Cvoid}), sa_ref, rmt)
218-
res = convert(Vector{String}, sa_ref[])
218+
res = collect(sa_ref[])
219219
free(sa_ref)
220220
res
221221
end
@@ -245,7 +245,7 @@ function push_refspecs(rmt::GitRemote)
245245
sa_ref = Ref(StrArrayStruct())
246246
@check ccall((:git_remote_get_push_refspecs, libgit2), Cint,
247247
(Ptr{StrArrayStruct}, Ptr{Cvoid}), sa_ref, rmt)
248-
res = convert(Vector{String}, sa_ref[])
248+
res = collect(sa_ref[])
249249
free(sa_ref)
250250
res
251251
end

stdlib/LibGit2/src/repository.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ function remotes(repo::GitRepo)
518518
@assert repo.ptr != C_NULL
519519
@check ccall((:git_remote_list, libgit2), Cint,
520520
(Ptr{StrArrayStruct}, Ptr{Cvoid}), sa_ref, repo)
521-
res = convert(Vector{String}, sa_ref[])
521+
res = collect(sa_ref[])
522522
free(sa_ref)
523523
return res
524524
end

stdlib/LibGit2/src/strarray.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# This file is a part of Julia. License is MIT: https://julialang.org/license
22

3-
43
function Base.cconvert(::Type{Ptr{StrArrayStruct}}, x::Vector)
54
str_ref = Base.cconvert(Ref{Cstring}, x)
65
sa_ref = Ref(StrArrayStruct(Base.unsafe_convert(Ref{Cstring}, str_ref), length(x)))
@@ -10,6 +9,8 @@ function Base.unsafe_convert(::Type{Ptr{StrArrayStruct}}, rr::Tuple{Ref{StrArray
109
Base.unsafe_convert(Ptr{StrArrayStruct}, first(rr))
1110
end
1211

13-
function Base.convert(::Type{Vector{String}}, sa::StrArrayStruct)
14-
[unsafe_string(unsafe_load(sa.strings, i)) for i = 1:sa.count]
12+
Base.length(sa::StrArrayStruct) = sa.count
13+
function Base.iterate(sa::StrArrayStruct, state=1)
14+
state > sa.count && return nothing
15+
(unsafe_string(unsafe_load(sa.strings, state)), state+1)
1516
end

stdlib/LibGit2/src/tag.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ function tag_list(repo::GitRepo)
1010
sa_ref = Ref(StrArrayStruct())
1111
@check ccall((:git_tag_list, libgit2), Cint,
1212
(Ptr{StrArrayStruct}, Ptr{Cvoid}), sa_ref, repo)
13-
res = convert(Vector{String}, sa_ref[])
13+
res = collect(sa_ref[])
1414
free(sa_ref)
1515
res
1616
end

stdlib/LibGit2/src/types.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ When fetching data from LibGit2, a typical usage would look like:
7878
```julia
7979
sa_ref = Ref(StrArrayStruct())
8080
@check ccall(..., (Ptr{StrArrayStruct},), sa_ref)
81-
res = convert(Vector{String}, sa_ref[])
81+
res = collect(sa_ref[])
8282
free(sa_ref)
8383
```
8484
In particular, note that `LibGit2.free` should be called afterward on the `Ref` object.

stdlib/LibGit2/test/libgit2-tests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ end
9595
p = ["XXX","YYY"]
9696
a = Base.cconvert(Ptr{LibGit2.StrArrayStruct}, p)
9797
b = Base.unsafe_convert(Ptr{LibGit2.StrArrayStruct}, a)
98-
@test p == convert(Vector{String}, unsafe_load(b))
98+
@test p == collect(unsafe_load(b))
9999
@noinline gcuse(a) = a
100100
gcuse(a)
101101
end

0 commit comments

Comments
 (0)