builtin: tag array methods unsafe: *_many, grow_len, pointers (#8983)

pull/8992/head
Nick Treleaven 2021-02-26 21:55:09 +00:00 committed by GitHub
parent 8874379c48
commit 23f231ee61
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 14 additions and 10 deletions

View File

@ -213,7 +213,7 @@ pub fn (mut ts TestSession) test() {
ts.nmessage_idx = 0 ts.nmessage_idx = 0
go ts.print_messages() go ts.print_messages()
pool_of_test_runners.set_shared_context(ts) pool_of_test_runners.set_shared_context(ts)
pool_of_test_runners.work_on_pointers(remaining_files.pointers()) pool_of_test_runners.work_on_pointers(unsafe { remaining_files.pointers() })
ts.benchmark.stop() ts.benchmark.stop()
ts.append_message(.sentinel, '') // send the sentinel ts.append_message(.sentinel, '') // send the sentinel
_ := <-ts.nprint_ended // wait for the stop of the printing thread _ := <-ts.nprint_ended // wait for the stop of the printing thread

View File

@ -156,6 +156,7 @@ pub fn (mut a array) insert(i int, val voidptr) {
} }
// insert_many inserts many values into the array from index `i`. // insert_many inserts many values into the array from index `i`.
[unsafe]
pub fn (mut a array) insert_many(i int, val voidptr, size int) { pub fn (mut a array) insert_many(i int, val voidptr, size int) {
$if !no_bounds_checking ? { $if !no_bounds_checking ? {
if i < 0 || i > a.len { if i < 0 || i > a.len {
@ -178,8 +179,9 @@ pub fn (mut a array) prepend(val voidptr) {
} }
// prepend_many prepends another array to this array. // prepend_many prepends another array to this array.
[unsafe]
pub fn (mut a array) prepend_many(val voidptr, size int) { pub fn (mut a array) prepend_many(val voidptr, size int) {
a.insert_many(0, val, size) unsafe { a.insert_many(0, val, size) }
} }
// delete deletes array element at index `i`. // delete deletes array element at index `i`.
@ -420,7 +422,7 @@ fn (mut a array) push(val voidptr) {
// push_many implements the functionality for pushing another array. // push_many implements the functionality for pushing another array.
// `val` is array.data and user facing usage is `a << [1,2,3]` // `val` is array.data and user facing usage is `a << [1,2,3]`
// TODO make private, right now it's used by strings.Builder [unsafe]
pub fn (mut a3 array) push_many(val voidptr, size int) { pub fn (mut a3 array) push_many(val voidptr, size int) {
if a3.data == val && !isnil(a3.data) { if a3.data == val && !isnil(a3.data) {
// handle `arr << arr` // handle `arr << arr`
@ -632,6 +634,7 @@ pub fn (mut a array) grow_cap(amount int) {
} }
// grow_len ensures that an array has a.len + amount of length // grow_len ensures that an array has a.len + amount of length
[unsafe]
pub fn (mut a array) grow_len(amount int) { pub fn (mut a array) grow_len(amount int) {
a.ensure_cap(a.len + amount) a.ensure_cap(a.len + amount)
a.len += amount a.len += amount
@ -728,6 +731,7 @@ pub fn compare_f32(a &f32, b &f32) int {
// pointers returns a new array, where each element // pointers returns a new array, where each element
// is the address of the corresponding element in the array. // is the address of the corresponding element in the array.
[unsafe]
pub fn (a array) pointers() []voidptr { pub fn (a array) pointers() []voidptr {
mut res := []voidptr{} mut res := []voidptr{}
for i in 0 .. a.len { for i in 0 .. a.len {

View File

@ -46,7 +46,7 @@ pub fn read_all(config ReadAllConfig) ?[]byte {
break break
} }
if b.len == read { if b.len == read {
b.grow_len(read_all_grow_len) unsafe { b.grow_len(read_all_grow_len) }
} }
} }
return b[..read] return b[..read]
@ -66,7 +66,7 @@ pub fn read_any(r Reader) ?[]byte {
break break
} }
if b.len == read { if b.len == read {
b.grow_len(read_all_grow_len) unsafe { b.grow_len(read_all_grow_len) }
} }
} }
return b[..read] return b[..read]

View File

@ -27,7 +27,7 @@ pub fn new_builder(initial_size int) Builder {
// write_bytes appends `bytes` to the accumulated buffer // write_bytes appends `bytes` to the accumulated buffer
[unsafe] [unsafe]
pub fn (mut b Builder) write_bytes(bytes byteptr, howmany int) { pub fn (mut b Builder) write_bytes(bytes byteptr, howmany int) {
b.buf.push_many(bytes, howmany) unsafe { b.buf.push_many(bytes, howmany) }
b.len += howmany b.len += howmany
} }
@ -50,7 +50,7 @@ pub fn (mut b Builder) write_string(s string) {
if s == '' { if s == '' {
return return
} }
b.buf.push_many(s.str, s.len) unsafe { b.buf.push_many(s.str, s.len) }
// for c in s { // for c in s {
// b.buf << c // b.buf << c
// } // }
@ -99,7 +99,7 @@ pub fn (mut b Builder) writeln(s string) {
// for c in s { // for c in s {
// b.buf << c // b.buf << c
// } // }
b.buf.push_many(s.str, s.len) unsafe { b.buf.push_many(s.str, s.len) }
// b.buf << []byte(s) // TODO // b.buf << []byte(s) // TODO
b.buf << `\n` b.buf << `\n`
b.len += s.len + 1 b.len += s.len + 1

View File

@ -74,7 +74,7 @@ pub fn (mut pool PoolProcessor) set_max_jobs(njobs int) {
// work_on_items returns *after* all threads finish. // work_on_items returns *after* all threads finish.
// You can optionally call get_results after that. // You can optionally call get_results after that.
pub fn (mut pool PoolProcessor) work_on_items<T>(items []T) { pub fn (mut pool PoolProcessor) work_on_items<T>(items []T) {
pool.work_on_pointers( items.pointers() ) pool.work_on_pointers( unsafe { items.pointers() } )
} }
pub fn (mut pool PoolProcessor) work_on_pointers(items []voidptr) { pub fn (mut pool PoolProcessor) work_on_pointers(items []voidptr) {

View File

@ -29,7 +29,7 @@ pub fn (mut ctx Context) write(s string) {
if s == '' { if s == '' {
return return
} }
ctx.print_buf.push_many(s.str, s.len) unsafe { ctx.print_buf.push_many(s.str, s.len) }
} }
[inline] [inline]