跳转到内容

std.Build.Cache.Directory

标准库Zig 0.17.0官方原文
源码std/Build/Cache/Directory.zig

第 1 行

字段

path: ?[]const u8

对于可直接操作已打开目录句柄的操作,此字段冗余, 但将目录传给子进程时需要使用它。 null 表示当前工作目录。

handle: Io.Dir

std.Build.Cache.Directory.Allocator

别名

第 8 行

别名目标 std.mem.Allocator

std.Build.Cache.Directory.assert

别名

第 6 行

别名目标 std.debug.assert

std.Build.Cache.Directory.clone

函数

第 16 行

pub fn clone(d: Directory, arena: Allocator) Allocator.Error!Directory

参数

d: Directory
arena: Allocator

错误

OutOfMemory

std.Build.Cache.Directory.closeAndFree

函数

第 55 行

是否应关闭句柄或释放路径由 具体使用方式决定;若调用方恰有此需求, 则可使用此便利函数。

pub fn closeAndFree(self: *Directory, gpa: Allocator, io: Io) void

参数

self: *Directory
gpa: Allocator
io: Io

std.Build.Cache.Directory.cwd

函数

第 23 行

pub fn cwd() Directory

std.Build.Cache.Directory.Directory

别名

第 1 行

别名目标 std.Build.Cache.Directory

std.Build.Cache.Directory.eql

函数

第 68 行

pub fn eql(self: Directory, other: Directory) bool

参数

self: Directory
other: Directory

std.Build.Cache.Directory.fmt

别名

第 7 行

别名目标 std.fmt

std.Build.Cache.Directory.format

函数

第 61 行

pub fn format(self: Directory, writer: *std.Io.Writer) std.Io.Writer.Error!void

参数

self: Directory
writer: *std.Io.Writer

错误

WriteFailed

详细诊断信息见 Writer 实现。

std.Build.Cache.Directory.fs

别名

第 5 行

别名目标 std.fs

std.Build.Cache.Directory.Io

别名

第 4 行

别名目标 std.Io

std.Build.Cache.Directory.join

函数

第 30 行

pub fn join(self: Directory, allocator: Allocator, paths: []const []const u8) ![]u8

参数

self: Directory
allocator: Allocator
paths: []const []const u8

std.Build.Cache.Directory.joinZ

函数

第 41 行

pub fn joinZ(self: Directory, allocator: Allocator, paths: []const []const u8) ![:0]u8

参数

self: Directory
allocator: Allocator
paths: []const []const u8

std.Build.Cache.Directory.std

别名

第 3 行

别名目标 std

源码

std/Build/Cache/Directory.zig

const Directory = @This();

const std = @import("../../std.zig");
const Io = std.Io;
const fs = std.fs;
const assert = std.debug.assert;
const fmt = std.fmt;
const Allocator = std.mem.Allocator;

/// 对于可直接操作已打开目录句柄的操作,此字段冗余,
/// 但将目录传给子进程时需要使用它。
/// `null` 表示当前工作目录。
path: ?[]const u8,
handle: Io.Dir,

pub fn clone(d: Directory, arena: Allocator) Allocator.Error!Directory {
    return .{
        .path = if (d.path) |p| try arena.dupe(u8, p) else null,
        .handle = d.handle,
    };
}

pub fn cwd() Directory {
    return .{
        .path = null,
        .handle = .cwd(),
    };
}

pub fn join(self: Directory, allocator: Allocator, paths: []const []const u8) ![]u8 {
    if (self.path) |p| {
        // TODO: 寻找仅需一次分配的简洁实现
        const part2 = try fs.path.join(allocator, paths);
        defer allocator.free(part2);
        return fs.path.join(allocator, &[_][]const u8{ p, part2 });
    } else {
        return fs.path.join(allocator, paths);
    }
}

pub fn joinZ(self: Directory, allocator: Allocator, paths: []const []const u8) ![:0]u8 {
    if (self.path) |p| {
        // TODO: 寻找仅需一次分配的简洁实现
        const part2 = try fs.path.join(allocator, paths);
        defer allocator.free(part2);
        return fs.path.joinZ(allocator, &[_][]const u8{ p, part2 });
    } else {
        return fs.path.joinZ(allocator, paths);
    }
}

/// 是否应关闭句柄或释放路径由
/// 具体使用方式决定;若调用方恰有此需求,
/// 则可使用此便利函数。
pub fn closeAndFree(self: *Directory, gpa: Allocator, io: Io) void {
    self.handle.close(io);
    if (self.path) |p| gpa.free(p);
    self.* = undefined;
}

pub fn format(self: Directory, writer: *std.Io.Writer) std.Io.Writer.Error!void {
    if (self.path) |p| {
        try writer.writeAll(p);
        try writer.writeAll(fs.path.sep_str);
    }
}

pub fn eql(self: Directory, other: Directory) bool {
    return self.handle.handle == other.handle.handle;
}