discard
Function parameters
Parameters
- io_reader:*std.Io.Reader
- limit:std.Io.Limit
Type definitions in this namespace
Types
- Permissions
- Cross-platform representation of permissions on a file.
- Reader
- Memoizes key information about a file handle such as:
Functions in this namespace
Functions
- close
- Upon success, the stream is in an uninitialized state.
- sync
- Blocks until all pending file contents and metadata modifications
- isTty
- Test whether the file refers to a terminal.
- getOrEnableAnsiEscapeSupport
- Returns whether or not ANSI escape codes will be treated as such,
- supportsAnsiEscapeCodes
- Test whether ANSI escape codes will be treated as such without
- setEndPos
- Shrinks or expands the file.
- seekBy
- Repositions read/write file offset relative to the current offset.
- seekFromEnd
- Repositions read/write file offset relative to the end.
- seekTo
- Repositions read/write file offset relative to the beginning.
- getPos
- TODO: integrate with async I/O
- getEndPos
- TODO: integrate with async I/O
- mode
- TODO: integrate with async I/O
- stat
- Returns `Stat` containing basic information about the `File`.
- chmod
- Changes the mode of the file.
- chown
- Changes the owner and group of the file.
- setPermissions
- Sets permissions according to the provided `Permissions` struct.
- updateTimes
- The underlying file system may have a different granularity than nanoseconds,
- readToEndAlloc
- Deprecated in favor of `Reader`.
- readToEndAllocOptions
- Deprecated in favor of `Reader`.
- readAll
- Deprecated in favor of `Reader`.
- pread
- On Windows, this function currently does alter the file pointer.
- preadAll
- Deprecated in favor of `Reader`.
- readv
- See https://github.com/ziglang/zig/issues/7699
- readvAll
- Deprecated in favor of `Reader`.
- preadv
- See https://github.com/ziglang/zig/issues/7699
- preadvAll
- Deprecated in favor of `Reader`.
- writeAll
- Deprecated in favor of `Writer`.
- pwrite
- On Windows, this function currently does alter the file pointer.
- pwriteAll
- Deprecated in favor of `Writer`.
- writev
- See https://github.com/ziglang/zig/issues/7699
- writevAll
- Deprecated in favor of `Writer`.
- pwritev
- See https://github.com/ziglang/zig/issues/7699
- pwritevAll
- Deprecated in favor of `Writer`.
- copyRange
- Deprecated in favor of `Writer`.
- copyRangeAll
- Deprecated in favor of `Writer`.
- deprecatedReader
- Deprecated in favor of `Reader`.
- deprecatedWriter
- Deprecated in favor of `Writer`.
- reader
- Defaults to positional reading; falls back to streaming.
- readerStreaming
- Positional is more threadsafe, since the global seek position is not
- writer
- Defaults to positional reading; falls back to streaming.
- writerStreaming
- Positional is more threadsafe, since the global seek position is not
- lock
- Blocks when an incompatible lock is held by another process.
- unlock
- Assumes the file is locked.
- tryLock
- Attempts to obtain a lock, returning `true` if the lock is
- downgradeLock
- Assumes the file is already locked in exclusive mode.
Error sets in this namespace
Error Sets
= posix.fd_t
Values
Source
Implementation
fn discard(io_reader: *std.Io.Reader, limit: std.Io.Limit) std.Io.Reader.Error!usize {
const r: *Reader = @alignCast(@fieldParentPtr("interface", io_reader));
const file = r.file;
const pos = r.pos;
switch (r.mode) {
.positional, .positional_reading => {
const size = r.getSize() catch {
r.mode = r.mode.toStreaming();
return 0;
};
const delta = @min(@intFromEnum(limit), size - pos);
r.pos = pos + delta;
return delta;
},
.streaming, .streaming_reading => {
// Unfortunately we can't seek forward without knowing the
// size because the seek syscalls provided to us will not
// return the true end position if a seek would exceed the
// end.
fallback: {
if (r.size_err == null and r.seek_err == null) break :fallback;
var trash_buffer: [128]u8 = undefined;
if (is_windows) {
const n = windows.ReadFile(file.handle, limit.slice(&trash_buffer), null) catch |err| {
r.err = err;
return error.ReadFailed;
};
if (n == 0) {
r.size = pos;
return error.EndOfStream;
}
r.pos = pos + n;
return n;
}
var iovecs: [max_buffers_len]std.posix.iovec = undefined;
var iovecs_i: usize = 0;
var remaining = @intFromEnum(limit);
while (remaining > 0 and iovecs_i < iovecs.len) {
iovecs[iovecs_i] = .{ .base = &trash_buffer, .len = @min(trash_buffer.len, remaining) };
remaining -= iovecs[iovecs_i].len;
iovecs_i += 1;
}
const n = posix.readv(file.handle, iovecs[0..iovecs_i]) catch |err| {
r.err = err;
return error.ReadFailed;
};
if (n == 0) {
r.size = pos;
return error.EndOfStream;
}
r.pos = pos + n;
return n;
}
const size = r.getSize() catch return 0;
const n = @min(size - pos, maxInt(i64), @intFromEnum(limit));
file.seekBy(n) catch |err| {
r.seek_err = err;
return 0;
};
r.pos = pos + n;
return n;
},
.failure => return error.ReadFailed,
}
}