use * for pointer type instead of &
See #770 To help automatically translate code, see the zig-fmt-pointer-reform-2 branch. This will convert all & into *. Due to the syntax ambiguity (which is why we are making this change), even address-of & will turn into *, so you'll have to manually fix thes instances. You will be guaranteed to get compile errors for them - expected 'type', found 'foo'
This commit is contained in:
80
std/io.zig
80
std/io.zig
@@ -34,20 +34,20 @@ pub fn getStdIn() GetStdIoErrs!File {
|
||||
|
||||
/// Implementation of InStream trait for File
|
||||
pub const FileInStream = struct {
|
||||
file: &File,
|
||||
file: *File,
|
||||
stream: Stream,
|
||||
|
||||
pub const Error = @typeOf(File.read).ReturnType.ErrorSet;
|
||||
pub const Stream = InStream(Error);
|
||||
|
||||
pub fn init(file: &File) FileInStream {
|
||||
pub fn init(file: *File) FileInStream {
|
||||
return FileInStream{
|
||||
.file = file,
|
||||
.stream = Stream{ .readFn = readFn },
|
||||
};
|
||||
}
|
||||
|
||||
fn readFn(in_stream: &Stream, buffer: []u8) Error!usize {
|
||||
fn readFn(in_stream: *Stream, buffer: []u8) Error!usize {
|
||||
const self = @fieldParentPtr(FileInStream, "stream", in_stream);
|
||||
return self.file.read(buffer);
|
||||
}
|
||||
@@ -55,20 +55,20 @@ pub const FileInStream = struct {
|
||||
|
||||
/// Implementation of OutStream trait for File
|
||||
pub const FileOutStream = struct {
|
||||
file: &File,
|
||||
file: *File,
|
||||
stream: Stream,
|
||||
|
||||
pub const Error = File.WriteError;
|
||||
pub const Stream = OutStream(Error);
|
||||
|
||||
pub fn init(file: &File) FileOutStream {
|
||||
pub fn init(file: *File) FileOutStream {
|
||||
return FileOutStream{
|
||||
.file = file,
|
||||
.stream = Stream{ .writeFn = writeFn },
|
||||
};
|
||||
}
|
||||
|
||||
fn writeFn(out_stream: &Stream, bytes: []const u8) !void {
|
||||
fn writeFn(out_stream: *Stream, bytes: []const u8) !void {
|
||||
const self = @fieldParentPtr(FileOutStream, "stream", out_stream);
|
||||
return self.file.write(bytes);
|
||||
}
|
||||
@@ -82,12 +82,12 @@ pub fn InStream(comptime ReadError: type) type {
|
||||
/// Return the number of bytes read. If the number read is smaller than buf.len, it
|
||||
/// means the stream reached the end. Reaching the end of a stream is not an error
|
||||
/// condition.
|
||||
readFn: fn (self: &Self, buffer: []u8) Error!usize,
|
||||
readFn: fn (self: *Self, buffer: []u8) Error!usize,
|
||||
|
||||
/// Replaces `buffer` contents by reading from the stream until it is finished.
|
||||
/// If `buffer.len()` would exceed `max_size`, `error.StreamTooLong` is returned and
|
||||
/// the contents read from the stream are lost.
|
||||
pub fn readAllBuffer(self: &Self, buffer: &Buffer, max_size: usize) !void {
|
||||
pub fn readAllBuffer(self: *Self, buffer: *Buffer, max_size: usize) !void {
|
||||
try buffer.resize(0);
|
||||
|
||||
var actual_buf_len: usize = 0;
|
||||
@@ -111,7 +111,7 @@ pub fn InStream(comptime ReadError: type) type {
|
||||
/// memory would be greater than `max_size`, returns `error.StreamTooLong`.
|
||||
/// Caller owns returned memory.
|
||||
/// If this function returns an error, the contents from the stream read so far are lost.
|
||||
pub fn readAllAlloc(self: &Self, allocator: &mem.Allocator, max_size: usize) ![]u8 {
|
||||
pub fn readAllAlloc(self: *Self, allocator: *mem.Allocator, max_size: usize) ![]u8 {
|
||||
var buf = Buffer.initNull(allocator);
|
||||
defer buf.deinit();
|
||||
|
||||
@@ -123,7 +123,7 @@ pub fn InStream(comptime ReadError: type) type {
|
||||
/// Does not include the delimiter in the result.
|
||||
/// If `buffer.len()` would exceed `max_size`, `error.StreamTooLong` is returned and the contents
|
||||
/// read from the stream so far are lost.
|
||||
pub fn readUntilDelimiterBuffer(self: &Self, buffer: &Buffer, delimiter: u8, max_size: usize) !void {
|
||||
pub fn readUntilDelimiterBuffer(self: *Self, buffer: *Buffer, delimiter: u8, max_size: usize) !void {
|
||||
try buffer.resize(0);
|
||||
|
||||
while (true) {
|
||||
@@ -145,7 +145,7 @@ pub fn InStream(comptime ReadError: type) type {
|
||||
/// memory would be greater than `max_size`, returns `error.StreamTooLong`.
|
||||
/// Caller owns returned memory.
|
||||
/// If this function returns an error, the contents from the stream read so far are lost.
|
||||
pub fn readUntilDelimiterAlloc(self: &Self, allocator: &mem.Allocator, delimiter: u8, max_size: usize) ![]u8 {
|
||||
pub fn readUntilDelimiterAlloc(self: *Self, allocator: *mem.Allocator, delimiter: u8, max_size: usize) ![]u8 {
|
||||
var buf = Buffer.initNull(allocator);
|
||||
defer buf.deinit();
|
||||
|
||||
@@ -156,43 +156,43 @@ pub fn InStream(comptime ReadError: type) type {
|
||||
/// Returns the number of bytes read. If the number read is smaller than buf.len, it
|
||||
/// means the stream reached the end. Reaching the end of a stream is not an error
|
||||
/// condition.
|
||||
pub fn read(self: &Self, buffer: []u8) !usize {
|
||||
pub fn read(self: *Self, buffer: []u8) !usize {
|
||||
return self.readFn(self, buffer);
|
||||
}
|
||||
|
||||
/// Same as `read` but end of stream returns `error.EndOfStream`.
|
||||
pub fn readNoEof(self: &Self, buf: []u8) !void {
|
||||
pub fn readNoEof(self: *Self, buf: []u8) !void {
|
||||
const amt_read = try self.read(buf);
|
||||
if (amt_read < buf.len) return error.EndOfStream;
|
||||
}
|
||||
|
||||
/// Reads 1 byte from the stream or returns `error.EndOfStream`.
|
||||
pub fn readByte(self: &Self) !u8 {
|
||||
pub fn readByte(self: *Self) !u8 {
|
||||
var result: [1]u8 = undefined;
|
||||
try self.readNoEof(result[0..]);
|
||||
return result[0];
|
||||
}
|
||||
|
||||
/// Same as `readByte` except the returned byte is signed.
|
||||
pub fn readByteSigned(self: &Self) !i8 {
|
||||
pub fn readByteSigned(self: *Self) !i8 {
|
||||
return @bitCast(i8, try self.readByte());
|
||||
}
|
||||
|
||||
pub fn readIntLe(self: &Self, comptime T: type) !T {
|
||||
pub fn readIntLe(self: *Self, comptime T: type) !T {
|
||||
return self.readInt(builtin.Endian.Little, T);
|
||||
}
|
||||
|
||||
pub fn readIntBe(self: &Self, comptime T: type) !T {
|
||||
pub fn readIntBe(self: *Self, comptime T: type) !T {
|
||||
return self.readInt(builtin.Endian.Big, T);
|
||||
}
|
||||
|
||||
pub fn readInt(self: &Self, endian: builtin.Endian, comptime T: type) !T {
|
||||
pub fn readInt(self: *Self, endian: builtin.Endian, comptime T: type) !T {
|
||||
var bytes: [@sizeOf(T)]u8 = undefined;
|
||||
try self.readNoEof(bytes[0..]);
|
||||
return mem.readInt(bytes, T, endian);
|
||||
}
|
||||
|
||||
pub fn readVarInt(self: &Self, endian: builtin.Endian, comptime T: type, size: usize) !T {
|
||||
pub fn readVarInt(self: *Self, endian: builtin.Endian, comptime T: type, size: usize) !T {
|
||||
assert(size <= @sizeOf(T));
|
||||
assert(size <= 8);
|
||||
var input_buf: [8]u8 = undefined;
|
||||
@@ -208,22 +208,22 @@ pub fn OutStream(comptime WriteError: type) type {
|
||||
const Self = this;
|
||||
pub const Error = WriteError;
|
||||
|
||||
writeFn: fn (self: &Self, bytes: []const u8) Error!void,
|
||||
writeFn: fn (self: *Self, bytes: []const u8) Error!void,
|
||||
|
||||
pub fn print(self: &Self, comptime format: []const u8, args: ...) !void {
|
||||
pub fn print(self: *Self, comptime format: []const u8, args: ...) !void {
|
||||
return std.fmt.format(self, Error, self.writeFn, format, args);
|
||||
}
|
||||
|
||||
pub fn write(self: &Self, bytes: []const u8) !void {
|
||||
pub fn write(self: *Self, bytes: []const u8) !void {
|
||||
return self.writeFn(self, bytes);
|
||||
}
|
||||
|
||||
pub fn writeByte(self: &Self, byte: u8) !void {
|
||||
pub fn writeByte(self: *Self, byte: u8) !void {
|
||||
const slice = (&byte)[0..1];
|
||||
return self.writeFn(self, slice);
|
||||
}
|
||||
|
||||
pub fn writeByteNTimes(self: &Self, byte: u8, n: usize) !void {
|
||||
pub fn writeByteNTimes(self: *Self, byte: u8, n: usize) !void {
|
||||
const slice = (&byte)[0..1];
|
||||
var i: usize = 0;
|
||||
while (i < n) : (i += 1) {
|
||||
@@ -234,14 +234,14 @@ pub fn OutStream(comptime WriteError: type) type {
|
||||
}
|
||||
|
||||
/// `path` needs to be copied in memory to add a null terminating byte, hence the allocator.
|
||||
pub fn writeFile(allocator: &mem.Allocator, path: []const u8, data: []const u8) !void {
|
||||
pub fn writeFile(allocator: *mem.Allocator, path: []const u8, data: []const u8) !void {
|
||||
var file = try File.openWrite(allocator, path);
|
||||
defer file.close();
|
||||
try file.write(data);
|
||||
}
|
||||
|
||||
/// On success, caller owns returned buffer.
|
||||
pub fn readFileAlloc(allocator: &mem.Allocator, path: []const u8) ![]u8 {
|
||||
pub fn readFileAlloc(allocator: *mem.Allocator, path: []const u8) ![]u8 {
|
||||
var file = try File.openRead(allocator, path);
|
||||
defer file.close();
|
||||
|
||||
@@ -265,13 +265,13 @@ pub fn BufferedInStreamCustom(comptime buffer_size: usize, comptime Error: type)
|
||||
|
||||
pub stream: Stream,
|
||||
|
||||
unbuffered_in_stream: &Stream,
|
||||
unbuffered_in_stream: *Stream,
|
||||
|
||||
buffer: [buffer_size]u8,
|
||||
start_index: usize,
|
||||
end_index: usize,
|
||||
|
||||
pub fn init(unbuffered_in_stream: &Stream) Self {
|
||||
pub fn init(unbuffered_in_stream: *Stream) Self {
|
||||
return Self{
|
||||
.unbuffered_in_stream = unbuffered_in_stream,
|
||||
.buffer = undefined,
|
||||
@@ -287,7 +287,7 @@ pub fn BufferedInStreamCustom(comptime buffer_size: usize, comptime Error: type)
|
||||
};
|
||||
}
|
||||
|
||||
fn readFn(in_stream: &Stream, dest: []u8) !usize {
|
||||
fn readFn(in_stream: *Stream, dest: []u8) !usize {
|
||||
const self = @fieldParentPtr(Self, "stream", in_stream);
|
||||
|
||||
var dest_index: usize = 0;
|
||||
@@ -338,12 +338,12 @@ pub fn BufferedOutStreamCustom(comptime buffer_size: usize, comptime OutStreamEr
|
||||
|
||||
pub stream: Stream,
|
||||
|
||||
unbuffered_out_stream: &Stream,
|
||||
unbuffered_out_stream: *Stream,
|
||||
|
||||
buffer: [buffer_size]u8,
|
||||
index: usize,
|
||||
|
||||
pub fn init(unbuffered_out_stream: &Stream) Self {
|
||||
pub fn init(unbuffered_out_stream: *Stream) Self {
|
||||
return Self{
|
||||
.unbuffered_out_stream = unbuffered_out_stream,
|
||||
.buffer = undefined,
|
||||
@@ -352,12 +352,12 @@ pub fn BufferedOutStreamCustom(comptime buffer_size: usize, comptime OutStreamEr
|
||||
};
|
||||
}
|
||||
|
||||
pub fn flush(self: &Self) !void {
|
||||
pub fn flush(self: *Self) !void {
|
||||
try self.unbuffered_out_stream.write(self.buffer[0..self.index]);
|
||||
self.index = 0;
|
||||
}
|
||||
|
||||
fn writeFn(out_stream: &Stream, bytes: []const u8) !void {
|
||||
fn writeFn(out_stream: *Stream, bytes: []const u8) !void {
|
||||
const self = @fieldParentPtr(Self, "stream", out_stream);
|
||||
|
||||
if (bytes.len >= self.buffer.len) {
|
||||
@@ -383,20 +383,20 @@ pub fn BufferedOutStreamCustom(comptime buffer_size: usize, comptime OutStreamEr
|
||||
|
||||
/// Implementation of OutStream trait for Buffer
|
||||
pub const BufferOutStream = struct {
|
||||
buffer: &Buffer,
|
||||
buffer: *Buffer,
|
||||
stream: Stream,
|
||||
|
||||
pub const Error = error{OutOfMemory};
|
||||
pub const Stream = OutStream(Error);
|
||||
|
||||
pub fn init(buffer: &Buffer) BufferOutStream {
|
||||
pub fn init(buffer: *Buffer) BufferOutStream {
|
||||
return BufferOutStream{
|
||||
.buffer = buffer,
|
||||
.stream = Stream{ .writeFn = writeFn },
|
||||
};
|
||||
}
|
||||
|
||||
fn writeFn(out_stream: &Stream, bytes: []const u8) !void {
|
||||
fn writeFn(out_stream: *Stream, bytes: []const u8) !void {
|
||||
const self = @fieldParentPtr(BufferOutStream, "stream", out_stream);
|
||||
return self.buffer.append(bytes);
|
||||
}
|
||||
@@ -407,7 +407,7 @@ pub const BufferedAtomicFile = struct {
|
||||
file_stream: FileOutStream,
|
||||
buffered_stream: BufferedOutStream(FileOutStream.Error),
|
||||
|
||||
pub fn create(allocator: &mem.Allocator, dest_path: []const u8) !&BufferedAtomicFile {
|
||||
pub fn create(allocator: *mem.Allocator, dest_path: []const u8) !*BufferedAtomicFile {
|
||||
// TODO with well defined copy elision we don't need this allocation
|
||||
var self = try allocator.create(BufferedAtomicFile);
|
||||
errdefer allocator.destroy(self);
|
||||
@@ -427,18 +427,18 @@ pub const BufferedAtomicFile = struct {
|
||||
}
|
||||
|
||||
/// always call destroy, even after successful finish()
|
||||
pub fn destroy(self: &BufferedAtomicFile) void {
|
||||
pub fn destroy(self: *BufferedAtomicFile) void {
|
||||
const allocator = self.atomic_file.allocator;
|
||||
self.atomic_file.deinit();
|
||||
allocator.destroy(self);
|
||||
}
|
||||
|
||||
pub fn finish(self: &BufferedAtomicFile) !void {
|
||||
pub fn finish(self: *BufferedAtomicFile) !void {
|
||||
try self.buffered_stream.flush();
|
||||
try self.atomic_file.finish();
|
||||
}
|
||||
|
||||
pub fn stream(self: &BufferedAtomicFile) &OutStream(FileOutStream.Error) {
|
||||
pub fn stream(self: *BufferedAtomicFile) *OutStream(FileOutStream.Error) {
|
||||
return &self.buffered_stream.stream;
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user