| ... | @@ -11,14 +11,23 @@ const io = std.io; | ... | @@ -11,14 +11,23 @@ const io = std.io; |
| 11 | /// For memory sources, if the supplied byte buffer is const, then `io.Writer` is not available. | 11 | /// For memory sources, if the supplied byte buffer is const, then `io.Writer` is not available. |
| 12 | /// The error set of the stream functions is the error set of the corresponding file functions. | 12 | /// The error set of the stream functions is the error set of the corresponding file functions. |
| 13 | pub const StreamSource = union(enum) { | 13 | pub const StreamSource = union(enum) { |
| | 14 | const has_file = (std.builtin.os.tag != .freestanding); |
| | 15 | |
| | 16 | /// The stream access is redirected to this buffer. |
| 14 | buffer: io.FixedBufferStream([]u8), | 17 | buffer: io.FixedBufferStream([]u8), |
| | 18 | |
| | 19 | /// The stream access is redirected to this buffer. |
| | 20 | /// Writing to the source will always yield `error.AccessDenied`. |
| 15 | const_buffer: io.FixedBufferStream([]const u8), | 21 | const_buffer: io.FixedBufferStream([]const u8), |
| 16 | file: std.fs.File, | | |
| 17 | | 22 | |
| 18 | pub const ReadError = std.fs.File.ReadError; | 23 | /// The stream access is redirected to this file. |
| 19 | pub const WriteError = std.fs.File.WriteError; | 24 | /// On freestanding, this must never be initialized! |
| 20 | pub const SeekError = std.fs.File.SeekError; | 25 | file: if (has_file) std.fs.File else void, |
| 21 | pub const GetSeekPosError = std.fs.File.GetSeekPosError; | 26 | |
| | 27 | pub const ReadError = io.FixedBufferStream([]u8).ReadError || (if (has_file) std.fs.File.ReadError else error{}); |
| | 28 | pub const WriteError = error{AccessDenied} || io.FixedBufferStream([]u8).WriteError || (if (has_file) std.fs.File.WriteError else error{}); |
| | 29 | pub const SeekError = io.FixedBufferStream([]u8).SeekError || (if (has_file) std.fs.File.SeekError else error{}); |
| | 30 | pub const GetSeekPosError = io.FixedBufferStream([]u8).GetSeekPosError || (if (has_file) std.fs.File.GetSeekPosError else error{}); |
| 22 | | 31 | |
| 23 | pub const Reader = io.Reader(*StreamSource, ReadError, read); | 32 | pub const Reader = io.Reader(*StreamSource, ReadError, read); |
| 24 | pub const Writer = io.Writer(*StreamSource, WriteError, write); | 33 | pub const Writer = io.Writer(*StreamSource, WriteError, write); |
| ... | @@ -36,7 +45,7 @@ pub const StreamSource = union(enum) { | ... | @@ -36,7 +45,7 @@ pub const StreamSource = union(enum) { |
| 36 | switch (self.*) { | 45 | switch (self.*) { |
| 37 | .buffer => |*x| return x.read(dest), | 46 | .buffer => |*x| return x.read(dest), |
| 38 | .const_buffer => |*x| return x.read(dest), | 47 | .const_buffer => |*x| return x.read(dest), |
| 39 | .file => |x| return x.read(dest), | 48 | .file => |x| if (!has_file) unreachable else return x.read(dest), |
| 40 | } | 49 | } |
| 41 | } | 50 | } |
| 42 | | 51 | |
| ... | @@ -44,7 +53,7 @@ pub const StreamSource = union(enum) { | ... | @@ -44,7 +53,7 @@ pub const StreamSource = union(enum) { |
| 44 | switch (self.*) { | 53 | switch (self.*) { |
| 45 | .buffer => |*x| return x.write(bytes), | 54 | .buffer => |*x| return x.write(bytes), |
| 46 | .const_buffer => return error.AccessDenied, | 55 | .const_buffer => return error.AccessDenied, |
| 47 | .file => |x| return x.write(bytes), | 56 | .file => |x| if (!has_file) unreachable else return x.write(bytes), |
| 48 | } | 57 | } |
| 49 | } | 58 | } |
| 50 | | 59 | |
| ... | @@ -52,7 +61,7 @@ pub const StreamSource = union(enum) { | ... | @@ -52,7 +61,7 @@ pub const StreamSource = union(enum) { |
| 52 | switch (self.*) { | 61 | switch (self.*) { |
| 53 | .buffer => |*x| return x.seekTo(pos), | 62 | .buffer => |*x| return x.seekTo(pos), |
| 54 | .const_buffer => |*x| return x.seekTo(pos), | 63 | .const_buffer => |*x| return x.seekTo(pos), |
| 55 | .file => |x| return x.seekTo(pos), | 64 | .file => |x| if (!has_file) unreachable else return x.seekTo(pos), |
| 56 | } | 65 | } |
| 57 | } | 66 | } |
| 58 | | 67 | |
| ... | @@ -60,7 +69,7 @@ pub const StreamSource = union(enum) { | ... | @@ -60,7 +69,7 @@ pub const StreamSource = union(enum) { |
| 60 | switch (self.*) { | 69 | switch (self.*) { |
| 61 | .buffer => |*x| return x.seekBy(amt), | 70 | .buffer => |*x| return x.seekBy(amt), |
| 62 | .const_buffer => |*x| return x.seekBy(amt), | 71 | .const_buffer => |*x| return x.seekBy(amt), |
| 63 | .file => |x| return x.seekBy(amt), | 72 | .file => |x| if (!has_file) unreachable else return x.seekBy(amt), |
| 64 | } | 73 | } |
| 65 | } | 74 | } |
| 66 | | 75 | |
| ... | @@ -68,7 +77,7 @@ pub const StreamSource = union(enum) { | ... | @@ -68,7 +77,7 @@ pub const StreamSource = union(enum) { |
| 68 | switch (self.*) { | 77 | switch (self.*) { |
| 69 | .buffer => |*x| return x.getEndPos(), | 78 | .buffer => |*x| return x.getEndPos(), |
| 70 | .const_buffer => |*x| return x.getEndPos(), | 79 | .const_buffer => |*x| return x.getEndPos(), |
| 71 | .file => |x| return x.getEndPos(), | 80 | .file => |x| if (!has_file) unreachable else return x.getEndPos(), |
| 72 | } | 81 | } |
| 73 | } | 82 | } |
| 74 | | 83 | |
| ... | @@ -76,7 +85,7 @@ pub const StreamSource = union(enum) { | ... | @@ -76,7 +85,7 @@ pub const StreamSource = union(enum) { |
| 76 | switch (self.*) { | 85 | switch (self.*) { |
| 77 | .buffer => |*x| return x.getPos(), | 86 | .buffer => |*x| return x.getPos(), |
| 78 | .const_buffer => |*x| return x.getPos(), | 87 | .const_buffer => |*x| return x.getPos(), |
| 79 | .file => |x| return x.getPos(), | 88 | .file => |x| if (!has_file) unreachable else return x.getPos(), |
| 80 | } | 89 | } |
| 81 | } | 90 | } |
| 82 | | 91 | |
| ... | @@ -92,3 +101,30 @@ pub const StreamSource = union(enum) { | ... | @@ -92,3 +101,30 @@ pub const StreamSource = union(enum) { |
| 92 | return .{ .context = self }; | 101 | return .{ .context = self }; |
| 93 | } | 102 | } |
| 94 | }; | 103 | }; |
| | 104 | |
| | 105 | test "StreamSource (refs)" { |
| | 106 | std.testing.refAllDecls(StreamSource); |
| | 107 | } |
| | 108 | |
| | 109 | test "StreamSource (mutable buffer)" { |
| | 110 | var buffer: [64]u8 = undefined; |
| | 111 | var source = StreamSource{ .buffer = std.io.fixedBufferStream(&buffer) }; |
| | 112 | |
| | 113 | var writer = source.writer(); |
| | 114 | |
| | 115 | try writer.writeAll("Hello, World!"); |
| | 116 | |
| | 117 | try std.testing.expectEqualStrings("Hello, World!", source.buffer.getWritten()); |
| | 118 | } |
| | 119 | |
| | 120 | test "StreamSource (const buffer)" { |
| | 121 | const buffer: [64]u8 = "Hello, World!".* ++ ([1]u8{0xAA} ** 51); |
| | 122 | var source = StreamSource{ .const_buffer = std.io.fixedBufferStream(&buffer) }; |
| | 123 | |
| | 124 | var reader = source.reader(); |
| | 125 | |
| | 126 | var dst_buffer: [13]u8 = undefined; |
| | 127 | try reader.readNoEof(&dst_buffer); |
| | 128 | |
| | 129 | try std.testing.expectEqualStrings("Hello, World!", &dst_buffer); |
| | 130 | } |