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

Support for UnsafeBSONString #15

Merged
merged 4 commits into from
Mar 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "LightBSON"
uuid = "a4a7f996-b3a6-4de6-b9db-2fa5f350df41"
authors = ["Christian Rorvik <christian.rorvik@gmail.com>"]
version = "0.2.5"
version = "0.2.6"

[deps]
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
Expand Down
4 changes: 2 additions & 2 deletions src/writer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ end

@inline wire_size_(x) = sizeof(x)
@inline wire_size_(x::Nothing) = 0
@inline wire_size_(x::String) = sizeof(x) + 5
@inline wire_size_(x::Union{String, UnsafeBSONString}) = sizeof(x) + 5
@inline wire_size_(x::BSONCode) = sizeof(x.code) + 5
@inline wire_size_(x::BSONSymbol) = sizeof(x.value) + 5
@inline wire_size_(x::UUID) = 21
Expand All @@ -49,7 +49,7 @@ end
@inline wire_store_(::Ptr{UInt8}, ::BSONMaxKey) = nothing
@inline wire_store_(::Ptr{UInt8}, ::BSONUndefined) = nothing

@inline function wire_store_(p::Ptr{UInt8}, x::String)
@inline function wire_store_(p::Ptr{UInt8}, x::Union{String, UnsafeBSONString})
unsafe_store!(Ptr{Int32}(p), (sizeof(x) + 1) % Int32)
GC.@preserve x unsafe_copyto!(p + 4, pointer(x), sizeof(x))
unsafe_store!(p + 4 + sizeof(x), 0x0)
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ using Sockets
using StructTypes
using Test
using UUIDs
using UnsafeArrays

struct EmptyStruct end

Expand Down
18 changes: 18 additions & 0 deletions test/writer_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,24 @@
BSONReader(buf, StrictBSONValidator())["x"][typeof(x)] == x
end

@testset "single field UnsafeBSONString" begin
buf = empty!(fill(0xff, 1000))
writer = BSONWriter(buf)
str = "teststr"
writer["x"] = UnsafeBSONString(pointer(str), length(str))
close(writer)
BSONReader(buf, StrictBSONValidator())["x"][String] == str
end

@testset "single field UnsafeBSONBinary" begin
buf = empty!(fill(0xff, 1000))
writer = BSONWriter(buf)
data = rand(UInt8, 10)
writer["x"] = UnsafeBSONBinary(UnsafeArray(pointer(data), (length(data),)))
close(writer)
BSONReader(buf, StrictBSONValidator())["x"][UnsafeBSONBinary] == data
end

@testset "document" begin
buf = empty!(fill(0xff, 1000))
writer = BSONWriter(buf)
Expand Down