diff --git a/src/bson.jl b/src/bson.jl index bdbfb2a..5c652a2 100644 --- a/src/bson.jl +++ b/src/bson.jl @@ -4,10 +4,10 @@ # # BSONType mirrors C enum bson_type_t. -primitive type BSONType 8 end # 1 byte +primitive type BSONType sizeof(Cint) * 8 end -Base.convert(::Type{T}, t::BSONType) where {T<:Number} = reinterpret(UInt8, t) -Base.convert(::Type{BSONType}, n::T) where {T<:Number} = reinterpret(BSONType, n) +Base.convert(::Type{T}, t::BSONType) where {T<:Number} = T(reinterpret(Cint, t)) +Base.convert(::Type{BSONType}, n::T) where {T<:Number} = reinterpret(BSONType, Cint(n)) BSONType(u::UInt8) = convert(BSONType, u) # @@ -40,10 +40,10 @@ const BSON_TYPE_MINKEY = BSONType(0xFF) # BSONSubType mirrors C enum bson_subtype_t. -primitive type BSONSubType 8 end +primitive type BSONSubType sizeof(Cint) * 8 end -Base.convert(::Type{T}, t::BSONSubType) where {T<:Number} = reinterpret(UInt8, t) -Base.convert(::Type{BSONSubType}, n::T) where {T<:Number} = reinterpret(BSONSubType, n) +Base.convert(::Type{T}, t::BSONSubType) where {T<:Number} = T(reinterpret(Cint, t)) +Base.convert(::Type{BSONSubType}, n::T) where {T<:Number} = reinterpret(BSONSubType, Cint(n)) BSONSubType(u::UInt8) = convert(BSONSubType, u) # diff --git a/test/bson_tests.jl b/test/bson_tests.jl index a35b71c..4751827 100644 --- a/test/bson_tests.jl +++ b/test/bson_tests.jl @@ -113,6 +113,26 @@ using Distributed @test bson["uuid2"] == uuid end + @testset "Binary data iteration (#145)" begin + # This would segfault on Julia 1.12 before the enum size fix + b = Mongoc.BSON("a" => UInt8[0x1, 0x2, 0x3]) + + # Test iteration + result = iterate(b) + @test result !== nothing + (pair, state) = result + @test pair.first == "a" + @test pair.second == UInt8[0x1, 0x2, 0x3] + + # Test Dict conversion (uses iteration internally) + d = Dict(b) + @test d["a"] == UInt8[0x1, 0x2, 0x3] + + # Test as_dict + d2 = Mongoc.as_dict(b) + @test d2["a"] == UInt8[0x1, 0x2, 0x3] + end + @testset "BSON key/values itr support" begin bson = Mongoc.BSON() bson["hey"] = 10