Function encode [src]
Caller owns returned memory.
Prototype
pub fn encode(allocator: std.mem.Allocator, value: anytype) ![]u8
Parameters
allocator: std.mem.Allocator
Example
test encode {
// https://lapo.it/asn1js/#MAgGAyoDBAIBBA
const Value = struct { a: asn1.Oid, b: i32 };
const test_case = .{
.value = Value{ .a = asn1.Oid.fromDotComptime("1.2.3.4"), .b = 4 },
.encoded = &[_]u8{ 0x30, 0x08, 0x06, 0x03, 0x2A, 0x03, 0x04, 0x02, 0x01, 0x04 },
};
const allocator = std.testing.allocator;
const actual = try encode(allocator, test_case.value);
defer allocator.free(actual);
try std.testing.expectEqualSlices(u8, test_case.encoded, actual);
}
Source
pub fn encode(allocator: std.mem.Allocator, value: anytype) ![]u8 {
var encoder = Encoder.init(allocator);
defer encoder.deinit();
try encoder.any(value);
return try encoder.buffer.toOwnedSlice();
}