pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/alexnask/interface.zig

crossorigen="anonymous" media="all" rel="stylesheet" href="https://github.githubassets.com/assets/repository-6534fbc3f5e83ac0.css" /> GitHub - alexnask/interface.zig: Dynamic dispatch for zig made easy · GitHub
Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Zig Interfaces

Easy solution for all your zig dynamic dispatch needs!

Features

  • Fully decoupled interfaces and implementations
  • Control over the storage/ownership of interface objects
  • Comptime support (including comptime-only interfaces)
  • Async function partial support (blocking on #4621)
  • Optional function support
  • Support for manually written vtables

Example

const interface = @import("interface.zig");
const Interface = interface.Interface;
const SelfType = interface.SelfType;

// Let us create a Reader interface.
// We wrap it in our own struct to make function calls more natural.
const Reader = struct {
    pub const ReadError = error { CouldNotRead };

    const IFace = Interface(struct {

        // Our interface requires a single non optional, non-const read function.
        read: fn (*SelfType, buf: []u8) ReadError!usize,

    }, interface.Storage.NonOwning); // This is a non owning interface, similar to Rust dyn traits.

    iface: IFace,

    // Wrap the interface's init, since the interface is non owning it requires no allocator argument.
    pub fn init(impl_ptr: var) Reader {
        return .{ .iface = try IFace.init(.{impl_ptr}) };
    }

    // Wrap the read function call
    pub fn read(self: *Reader, buf: []u8) ReadError!usize {
        return self.iface.call("read", .{buf});
    }

    // Define additional, non-dynamic functions!
    pub fn readAll(self: *Self, buf: []u8) ReadError!usize {
        var index: usize = 0;
        while (index != buf.len) {
            const partial_amt = try self.read(buffer[index..]);
            if (partial_amt == 0) return index;
            index += partial_amt;
        }
        return index;
    }
};

// Let's create an example reader
const ExampleReader = struct {
    state: u8,

    // Note that this reader cannot return an error, the return type
    // of our implementation functions only needs to coerce to the
    // interface's function return type.
    pub fn read(self: ExampleReader, buf: []u8) usize {
        for (buf) |*c| {
            c.* = self.state;
        }
        return buf.len;
    }
};

test "Use our reader interface!" {
    var example_reader = ExampleReader{ .state=42 };

    var reader = Reader.init(&example_reader);

    var buf: [100]u8 = undefined;
    _ = reader.read(&buf) catch unreachable;
}

See examples.zig for more examples.

Releases

Packages

Contributors

Languages

pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy