From 69e129a7984827b16b61dc99d573a28100eaac00 Mon Sep 17 00:00:00 2001 From: Romain Poncet Date: Wed, 5 Oct 2022 09:43:57 +0100 Subject: [PATCH] bson_simple for simple namedtuples (#25) --- Project.toml | 2 +- src/LightBSON.jl | 1 + src/writer.jl | 10 ---------- 3 files changed, 2 insertions(+), 11 deletions(-) diff --git a/Project.toml b/Project.toml index d4d1cc2..9524d43 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "LightBSON" uuid = "a4a7f996-b3a6-4de6-b9db-2fa5f350df41" authors = ["Christian Rorvik "] -version = "0.2.14" +version = "0.2.15" [deps] DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8" diff --git a/src/LightBSON.jl b/src/LightBSON.jl index e83ba7a..ec47a06 100644 --- a/src/LightBSON.jl +++ b/src/LightBSON.jl @@ -65,6 +65,7 @@ bson_schema_version(::Type{T}) where T = nothing bson_schema_version_field(::Type{T}) where T = "_v" bson_simple(::Type{T}) where T = StructTypes.StructType(T) == StructTypes.NoStructType() +bson_simple(::Type{<:NamedTuple}) = true include("object_id.jl") include("types.jl") diff --git a/src/writer.jl b/src/writer.jl index ecc58f4..7790332 100644 --- a/src/writer.jl +++ b/src/writer.jl @@ -199,16 +199,6 @@ function Base.setindex!(writer::BSONWriter, fields::Tuple{Vararg{Pair{String, T} nothing end -@generated function Base.setindex!(writer::BSONWriter, fields::T) where T <: NamedTuple - e = Expr(:block) - for fn in fieldnames(T) - fns = "$fn" - push!(e.args, :(writer[$fns] = fields.$fn)) - end - push!(e.args, nothing) - e -end - @inline @generated function bson_write_simple(writer::BSONWriter, value::T) where T fieldcount(T) == 0 && return nothing totalsize = sum(wire_size_, fieldtypes(T)) + sum(sizeof, fieldnames(T)) + fieldcount(T) * 2