0

I'm interacting with the SDL zig bindings

  • I have a Surface, which contains a pointer to some pixels. In C, this is a void*, in zig it is an *anyopaque
  • I want to pass this pointer-to-pixels to the UpdateTexture function, which in C accepts a void* and in zig accepts a []const u8

@ptrCast gives me "illegal pointer cast to slice", so what is the correct way to treat an opaque pointer as a slice?

Shish
  • 2,773
  • 19
  • 16

1 Answers1

1

Cast *anyopaque to a many-item pointer first, then make it into a slice. E.g.

var anyopaque_pointer: *anyopaque = ...
var item_count: usize = ...

var result: []const u8 = @ptrCast([*]u8, anyopaque_pointer)[0..item_count];
sigod
  • 3,514
  • 2
  • 21
  • 44