From bf611f3106541551e6e1eec0adf73af5d13fda1f Mon Sep 17 00:00:00 2001 From: Joshua Suskalo Date: Wed, 15 Sep 2021 21:32:47 -0500 Subject: [PATCH] Add general serialization function --- src/coffi/ffi.clj | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/coffi/ffi.clj b/src/coffi/ffi.clj index 53050e7..fa2f536 100644 --- a/src/coffi/ffi.clj +++ b/src/coffi/ffi.clj @@ -303,6 +303,19 @@ [obj _type segment _scope] (MemoryAccess/setAddress segment obj)) +(defn serialize + "Serializes an arbitrary type. + + For types which have a primitive representation, this serializes into that + representation. For types which do not, it allocates a new segment and + serializes into that." + ([obj type] (serialize obj type (connected-scope))) + ([obj type scope] + (if (primitive-type type) + (serialize* obj type scope) + (let [segment (alloc-instance type scope)] + (serialize-into obj type segment scope))))) + (defmulti deserialize-from "Deserializes the given segment into a Clojure data structure." (fn @@ -385,11 +398,6 @@ deserialize-from) obj type)) -(defn serialize - "Serializes the `obj` into a newly-allocated [[MemorySegment]]." - ([obj type] (serialize obj type (ResourceScope/newImplicitScope))) - ([obj type scope] (serialize-into obj type (alloc-instance type scope) scope))) - (defn load-system-library "Loads the library named `libname` from the system's load path." [libname]