Last active
February 13, 2019 21:42
-
-
Save schmee/5a685f7a65961828838b2f0a798e66f2 to your computer and use it in GitHub Desktop.
Using example syntax
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"self" is | |
struct { | |
elems: []T, | |
count: usize, | |
allocator: *Allocator | |
} | |
without using: | |
fn ensureCapacity(self: *Self, cap: usize) void { | |
if (self.count + cap < self.elems.len) { | |
warn("Growing array from {}", self.elems.len); | |
var old_elems = self.elems; | |
var new_elems = self.allocator.alloc(T, self.elems.len * 2); | |
mem.copy(T, new_elems[0..], self.elems[0..]); | |
self.elems = new_elems; | |
self.allocator.free(elems); | |
} | |
} | |
with strawman "using" syntax: | |
fn ensureCapacity(self: *Self, cap: usize) void { | |
using self | |
if (count + cap < elems.len) { | |
warn("Growing array from {}", elems.len); | |
var old_elems = elems; | |
var new_elems = allocator.alloc(T, elems.len * 2); | |
mem.copy(T, new_elems[0..], elems[0..]); | |
elems = new_elems; | |
allocator.free(elems); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment