Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use standard Julia mechanisms for LibGit2 StrArrayStruct and Buffer types. #19962

Merged
merged 8 commits into from
Jan 18, 2017

Conversation

simonbyrne
Copy link
Contributor

@simonbyrne simonbyrne commented Jan 10, 2017

The code currently calls Libc.malloc, and then relies on the library
function to free the data. This seems slightly dubious, so I rejigged it
to use cconvert and unsafe_convert.

@simonbyrne simonbyrne added the libgit2 The libgit2 library or the LibGit2 stdlib module label Jan 10, 2017
end
return arr
function Base.unsafe_convert(::Type{Ptr{StrArrayStruct}}, tup::Tuple)
Base.unsafe_convert(Ptr{StrArrayStruct}, first(tup))
Copy link
Contributor

Choose a reason for hiding this comment

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

why does this throw away all but the first element?

```
In particular, note that `LibGit2.free` should be called afterward on the `Ref` object.

Conversely, when a vector of strings to LibGit2, it is generally simplest to rely on
Copy link
Contributor

Choose a reason for hiding this comment

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

when passing?

data = [Base.cconvert(Cstring, s) for s in strs]
ptrs = [Base.unsafe_convert(Cstring, d) for d in data]
sa = Ref(StrArrayStruct(pointer(ptrs), length(ptrs)))
sa, data, ptrs
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@tkelman related to your comment below: the idea is that cconvert returns a tuple but maintains a reference to the original data.

I agree it's not very pretty: any ideas for a better way to do it?





Copy link
Member

Choose a reason for hiding this comment

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

Really pedantic point, but perhaps 4 line breaks is an unnecessary amount of vertical space?

@kwdef immutable StrArrayStruct

When fetching data from LibGit2, a typical usage would look like:
```
Copy link
Member

@ararslan ararslan Jan 10, 2017

Choose a reason for hiding this comment

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

```julia? If that seems a worthwhile change, it could be applied to other blocks in the docstrings as well.

end
return arr
function Base.cconvert(::Type{Ptr{StrArrayStruct}}, x::Vector)
str_ref = Base.cconvert(Ref{Cstring}, x)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@vtjnash Does this look okay?

Copy link
Member

Choose a reason for hiding this comment

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

yes

(Ptr{StrArrayStruct}, Ptr{StrArrayStruct}),
dst_ptr, Ref(src))
return dst_ptr[]
function Base.unsafe_convert(::Type{Vector{String}}, sa::StrArrayStruct)
Copy link
Member

Choose a reason for hiding this comment

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

I think this is simply convert, no unsafe_ prefix necessary.

@simonbyrne
Copy link
Contributor Author

Okay, I'm fairly happy with this now. Thanks everyone.

@simonbyrne simonbyrne merged commit 48e923f into master Jan 18, 2017
@simonbyrne simonbyrne deleted the sb/libgit2/strarraystruct branch January 18, 2017 11:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libgit2 The libgit2 library or the LibGit2 stdlib module
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants