Skip to content

Commit

Permalink
Fixing bytebuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
lerno committed Feb 12, 2025
1 parent 5d51799 commit 4795839
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
16 changes: 8 additions & 8 deletions lib/std/io/stream/bytebuffer.c3
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ struct ByteBuffer (InStream, OutStream)
max_read defines how many bytes might be kept before its internal buffer is shrinked.
@require self.bytes.len == 0 "Buffer already initialized."
*>
fn ByteBuffer*! ByteBuffer.new_init(&self, usz max_read, usz initial_capacity = 16, Allocator allocator = allocator::heap())
fn ByteBuffer* ByteBuffer.new_init(&self, usz max_read, usz initial_capacity = 16, Allocator allocator = allocator::heap())
{
*self = { .allocator = allocator, .max_read = max_read };
initial_capacity = max(initial_capacity, 16);
self.grow(initial_capacity)!;
self.grow(initial_capacity);
return self;
}

fn ByteBuffer*! ByteBuffer.temp_init(&self, usz max_read, usz initial_capacity = 16)
fn ByteBuffer* ByteBuffer.temp_init(&self, usz max_read, usz initial_capacity = 16)
{
return self.new_init(max_read, initial_capacity, allocator::temp());
}
Expand All @@ -33,7 +33,7 @@ fn ByteBuffer*! ByteBuffer.temp_init(&self, usz max_read, usz initial_capacity =
@require buf.len > 0
@require self.bytes.len == 0 "Buffer already initialized."
*>
fn ByteBuffer*! ByteBuffer.init_with_buffer(&self, char[] buf)
fn ByteBuffer* ByteBuffer.init_with_buffer(&self, char[] buf)
{
*self = { .max_read = buf.len, .bytes = buf };
return self;
Expand All @@ -48,7 +48,7 @@ fn void ByteBuffer.free(&self)
fn usz! ByteBuffer.write(&self, char[] bytes) @dynamic
{
usz cap = self.bytes.len - self.write_idx;
if (cap < bytes.len) self.grow(bytes.len)!;
if (cap < bytes.len) self.grow(bytes.len);
self.bytes[self.write_idx:bytes.len] = bytes[..];
self.write_idx += bytes.len;
return bytes.len;
Expand All @@ -57,7 +57,7 @@ fn usz! ByteBuffer.write(&self, char[] bytes) @dynamic
fn void! ByteBuffer.write_byte(&self, char c) @dynamic
{
usz cap = self.bytes.len - self.write_idx;
if (cap == 0) self.grow(1)!;
if (cap == 0) self.grow(1);
self.bytes[self.write_idx] = c;
self.write_idx++;
}
Expand Down Expand Up @@ -128,10 +128,10 @@ fn usz! ByteBuffer.available(&self) @inline @dynamic
return self.write_idx - self.read_idx;
}

fn void! ByteBuffer.grow(&self, usz n)
fn void ByteBuffer.grow(&self, usz n)
{
n = math::next_power_of_2(n);
char* p = allocator::realloc(self.allocator, self.bytes, n)!;
char* p = allocator::realloc(self.allocator, self.bytes, n);
self.bytes = p[:n];
}

Expand Down
2 changes: 1 addition & 1 deletion test/unit/stdlib/io/bytebuffer.c3
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import std::io;
fn void write_read()
{
ByteBuffer buffer;
buffer.new_init(0)!!;
buffer.new_init(0);
defer buffer.free();
buffer.write("hello")!!;

Expand Down
2 changes: 1 addition & 1 deletion test/unit/stdlib/io/varint.c3
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import std::io;
fn void write_read()
{
ByteBuffer buf;
buf.temp_init(16)!!;
buf.temp_init(16);
usz n;
uint x;
uint y;
Expand Down

0 comments on commit 4795839

Please sign in to comment.