Function zvariant::to_writer

source ·
pub unsafe fn to_writer<W, T>(
    writer: &mut W,
    ctxt: Context,
    value: &T
) -> Result<Written>
where W: Write + Seek, T: ?Sized + Serialize + DynamicType,
Expand description

Serialize T to the given writer.


use zvariant::{serialized::{Context, Data}, to_writer, LE};

let ctxt = Context::new_dbus(LE, 0);
let mut cursor = std::io::Cursor::new(vec![]);
// SAFETY: No FDs are being serialized here so its completely safe.
unsafe { to_writer(&mut cursor, ctxt, &42u32) }.unwrap();
let encoded = Data::new(cursor.get_ref(), ctxt);
let value: u32 = encoded.deserialize().unwrap().0;
assert_eq!(value, 42);


On Unix systems, the returned Written instance can contain file descriptors and therefore the caller is responsible for not dropping the returned Written instance before the writer. Otherwise, the file descriptors in the Written instance will be closed while serialized data will still refer to them. Hence why this function is marked unsafe.

On non-Unix systems, the returned Written instance will not contain any file descriptors and hence is safe to drop.