| ... | ... | @@ -39,5 +39,32 @@ pub fn FixedBufferStream(comptime Buffer: type) type { |
| 39 | 39 | .state = @ptrCast(self), |
| 40 | 40 | }; |
| 41 | 41 | } |
| 42 | |
| 43 | pub const WriteError = error{NoSpaceLeft}; |
| 44 | pub usingnamespace nio.Writable(@This(), ._var); |
| 45 | /// If the returned number of bytes written is less than requested, the buffer is full. |
| 46 | /// Returns `error.NoSpaceLeft` when no bytes would be written. |
| 47 | pub fn write(self: *Self, bytes: []const u8) WriteError!usize { |
| 48 | if (bytes.len == 0) return 0; |
| 49 | if (self.pos >= self.buffer.len) return error.NoSpaceLeft; |
| 50 | const n = @min(self.buffer.len - self.pos, bytes.len); |
| 51 | @memcpy(self.buffer[self.pos..][0..n], bytes[0..n]); |
| 52 | self.pos += n; |
| 53 | if (n == 0) return error.NoSpaceLeft; |
| 54 | return n; |
| 55 | } |
| 56 | |
| 57 | pub fn anyWritable(self: *Self) nio.AnyWritable { |
| 58 | const S = struct { |
| 59 | fn write(s: *allowzero anyopaque, buffer: []u8) anyerror!usize { |
| 60 | const fbs: *Self = @ptrCast(@alignCast(s)); |
| 61 | return fbs.write(buffer); |
| 62 | } |
| 63 | }; |
| 64 | return .{ |
| 65 | .vtable = &.{ .write = S.write }, |
| 66 | .state = @ptrCast(self), |
| 67 | }; |
| 68 | } |
| 42 | 69 | }; |
| 43 | 70 | } |