summaryrefslogtreecommitdiff
path: root/pkg/macos/foundation/data.zig
blob: c06da65b728ea7b36c16fb5d3cd4e954e62c0149 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
const std = @import("std");
const Allocator = std.mem.Allocator;
const foundation = @import("../foundation.zig");
const c = @import("c.zig").c;

pub const Data = opaque {
    pub fn createWithBytesNoCopy(data: []const u8) Allocator.Error!*Data {
        return @as(
            ?*Data,
            @ptrFromInt(@intFromPtr(c.CFDataCreateWithBytesNoCopy(
                null,
                data.ptr,
                @intCast(data.len),
                c.kCFAllocatorNull,
            ))),
        ) orelse error.OutOfMemory;
    }

    pub fn release(self: *Data) void {
        foundation.CFRelease(self);
    }

    pub fn getPointer(self: *Data) [*]const u8 {
        return @ptrCast(c.CFDataGetBytePtr(@ptrCast(self)));
    }

    pub fn getLength(self: *Data) usize {
        return @intCast(c.CFDataGetLength(@ptrCast(self)));
    }
};

test {
    //const testing = std.testing;

    const raw = "hello world";
    const data = try Data.createWithBytesNoCopy(raw);
    defer data.release();
}