Returns a serialization configuration, which may be set on a Socket for
custom serialization and unserialization of non-system reference objects,
allowing these to be sent and received between different R sessions. This
utilises the 'refhook' system of R native serialization. Once set, the
functions apply to all send and receive operations performed in mode
"serial"
over the Socket or Context created from the Socket.
Arguments
- class
character string of the class of object custom serialization functions are applied to, e.g. 'ArrowTabular' or 'torch_tensor'.
- sfunc
a function that accepts a reference object inheriting from
class
(or a list of such objects) and returns a raw vector.- ufunc
a function that accepts a raw vector and returns a reference object (or list of such objects).
- vec
[default FALSE] whether or not the serialization functions are vectorized. If
FALSE
, they should accept and return reference objects individually e.g.arrow::write_to_raw
andarrow::read_ipc_stream
. IfTRUE
, they should accept and return a list of reference objects, e.g.torch::torch_serialize
andtorch::torch_load
.
Value
A list comprising the configuration. This should be set on a Socket
using opt<-()
with option name "serial"
.
Examples
cfg <- serial_config("test_cls", function(x) serialize(x, NULL), unserialize)
cfg
#> [[1]]
#> [1] "test_cls"
#>
#> [[2]]
#> function (x)
#> serialize(x, NULL)
#> <environment: 0x5564008efe60>
#>
#> [[3]]
#> function (connection, refhook = NULL)
#> {
#> if (typeof(connection) != "raw" && !is.character(connection) &&
#> !inherits(connection, "connection"))
#> stop("'connection' must be a connection")
#> .Internal(unserialize(connection, refhook))
#> }
#> <bytecode: 0x5564008cbad0>
#> <environment: namespace:base>
#>
#> [[4]]
#> [1] FALSE
#>
s <- socket()
opt(s, "serial") <- cfg
# provide an empty list to remove registered functions
opt(s, "serial") <- list()
close(s)