Skip to content

Commit

Permalink
Simplify FFI
Browse files Browse the repository at this point in the history
  • Loading branch information
Drvi committed Dec 12, 2023
1 parent 27b56fc commit db7231c
Showing 1 changed file with 15 additions and 18 deletions.
33 changes: 15 additions & 18 deletions src/ObjectStore.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ const _INIT_LOCK::ReentrantLock = ReentrantLock()
function init_rust_store()
Base.@lock _INIT_LOCK begin
if RUST_STORE_STARTED[]
return
end
@ccall rust_lib.start()::Cint
return
end
@ccall rust_lib.start()::Cint
RUST_STORE_STARTED[] = true
end
end
Expand All @@ -55,23 +55,22 @@ struct AzureCredentials
key::String
host::String
end
const _AzureCredentialsFFI = NTuple{4,Cstring}

struct AzureCredentialsFFI
account::Cstring
container::Cstring
key::Cstring
host::Cstring
end

function to_ffi(credentials::AzureCredentials)
AzureCredentialsFFI(
function Base.cconvert(::Type{Ref{AzureCredentials}}, credentials::AzureCredentials)
credentials_ffi = (
Base.unsafe_convert(Cstring, Base.cconvert(Cstring, credentials.account)),
Base.unsafe_convert(Cstring, Base.cconvert(Cstring, credentials.container)),
Base.unsafe_convert(Cstring, Base.cconvert(Cstring, credentials.key)),
Base.unsafe_convert(Cstring, Base.cconvert(Cstring, credentials.host))
)
)::_AzureCredentialsFFI
# cconvert ensures its outputs are preserved during a ccall, so we can crate a pointer
# safely in the unsafe_convert call.
return credentials_ffi, Ref(credentials_ffi)
end
function Base.unsafe_convert(::Type{Ref{AzureCredentials}}, x::Tuple{T,Ref{T}}) where {T<:_AzureCredentialsFFI}
return Base.unsafe_convert(Ptr{_AzureCredentialsFFI}, x[2])
end


struct Response
result::Cint
Expand All @@ -83,7 +82,6 @@ end

function blob_get!(path::String, buffer::AbstractVector{UInt8}, credentials::AzureCredentials)
response = Ref(Response())
credentials_ffi = Ref(to_ffi(credentials))
size = length(buffer)
cond = Base.AsyncCondition()
cond_handle = cond.handle
Expand All @@ -92,7 +90,7 @@ function blob_get!(path::String, buffer::AbstractVector{UInt8}, credentials::Azu
path::Cstring,
buffer::Ref{Cuchar},
size::Culonglong,
credentials_ffi::Ref{AzureCredentialsFFI},
credentials::Ref{AzureCredentials},
response::Ref{Response},
cond_handle::Ptr{Cvoid}
)::Cint
Expand Down Expand Up @@ -121,7 +119,6 @@ end

function blob_put!(path::String, buffer::AbstractVector{UInt8}, credentials::AzureCredentials)
response = Ref(Response())
credentials_ffi = Ref(to_ffi(credentials))
size = length(buffer)
cond = Base.AsyncCondition()
cond_handle = cond.handle
Expand All @@ -130,7 +127,7 @@ function blob_put!(path::String, buffer::AbstractVector{UInt8}, credentials::Azu
path::Cstring,
buffer::Ref{Cuchar},
size::Culonglong,
credentials_ffi::Ref{AzureCredentialsFFI},
credentials::Ref{AzureCredentials},
response::Ref{Response},
cond_handle::Ptr{Cvoid}
)::Cint
Expand Down

0 comments on commit db7231c

Please sign in to comment.