Skip to content

Proposal: BufMap.set() returns the copied value #8533

@akvadrako

Description

@akvadrako

Currently BufMap.set(key, value) copies the value and stores it, but it only returns errors. BufMap is probably being used because value won't live long and a copy is desired for further processing. So now you might need to do this:

fn saveValue(key: []u8, value: []const u8) ![]const u8 {
    try map.set(key, value);
    if (map.get(key)) |v| {
      storePointer(v);
      return v;
    }
    unreachable;
}

If set would return the copy one could do this instead:

fn saveValue(key: []u8, value: []const u8) ![]const u8 {
    const v = try map.set(key, value);
    storePointer(v);
    return v;
}

BTW I've just started using Zig last week so I could certainly be missing something in how this is supposed to be used.

Metadata

Metadata

Assignees

No one assigned

    Labels

    proposalThis issue suggests modifications. If it also has the "accepted" label then it is planned.standard libraryThis issue involves writing Zig code for the standard library.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions