From 23f231ee61a12d51afd6948085a2c5ca4ebfe4bf Mon Sep 17 00:00:00 2001 From: Nick Treleaven Date: Fri, 26 Feb 2021 21:55:09 +0000 Subject: [PATCH] builtin: tag array methods unsafe: *_many, grow_len, pointers (#8983) --- cmd/tools/modules/testing/common.v | 2 +- vlib/builtin/array.v | 8 ++++++-- vlib/io/reader.v | 4 ++-- vlib/strings/builder.v | 6 +++--- vlib/sync/pool/pool.v | 2 +- vlib/term/ui/ui.v | 2 +- 6 files changed, 14 insertions(+), 10 deletions(-) diff --git a/cmd/tools/modules/testing/common.v b/cmd/tools/modules/testing/common.v index 0420e58c2e..3cf4e846bf 100644 --- a/cmd/tools/modules/testing/common.v +++ b/cmd/tools/modules/testing/common.v @@ -213,7 +213,7 @@ pub fn (mut ts TestSession) test() { ts.nmessage_idx = 0 go ts.print_messages() 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.append_message(.sentinel, '') // send the sentinel _ := <-ts.nprint_ended // wait for the stop of the printing thread diff --git a/vlib/builtin/array.v b/vlib/builtin/array.v index fc71d1e043..7b93ec0641 100644 --- a/vlib/builtin/array.v +++ b/vlib/builtin/array.v @@ -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`. +[unsafe] pub fn (mut a array) insert_many(i int, val voidptr, size int) { $if !no_bounds_checking ? { 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. +[unsafe] 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`. @@ -420,7 +422,7 @@ fn (mut a array) push(val voidptr) { // push_many implements the functionality for pushing another array. // `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) { if a3.data == val && !isnil(a3.data) { // 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 +[unsafe] pub fn (mut a array) grow_len(amount int) { a.ensure_cap(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 // is the address of the corresponding element in the array. +[unsafe] pub fn (a array) pointers() []voidptr { mut res := []voidptr{} for i in 0 .. a.len { diff --git a/vlib/io/reader.v b/vlib/io/reader.v index ccbbb22e5c..588dd5a550 100644 --- a/vlib/io/reader.v +++ b/vlib/io/reader.v @@ -46,7 +46,7 @@ pub fn read_all(config ReadAllConfig) ?[]byte { break } if b.len == read { - b.grow_len(read_all_grow_len) + unsafe { b.grow_len(read_all_grow_len) } } } return b[..read] @@ -66,7 +66,7 @@ pub fn read_any(r Reader) ?[]byte { break } if b.len == read { - b.grow_len(read_all_grow_len) + unsafe { b.grow_len(read_all_grow_len) } } } return b[..read] diff --git a/vlib/strings/builder.v b/vlib/strings/builder.v index 54c08fbbd9..24d17fe1ca 100644 --- a/vlib/strings/builder.v +++ b/vlib/strings/builder.v @@ -27,7 +27,7 @@ pub fn new_builder(initial_size int) Builder { // write_bytes appends `bytes` to the accumulated buffer [unsafe] 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 } @@ -50,7 +50,7 @@ pub fn (mut b Builder) write_string(s string) { if s == '' { return } - b.buf.push_many(s.str, s.len) + unsafe { b.buf.push_many(s.str, s.len) } // for c in s { // b.buf << c // } @@ -99,7 +99,7 @@ pub fn (mut b Builder) writeln(s string) { // for c in s { // 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 << `\n` b.len += s.len + 1 diff --git a/vlib/sync/pool/pool.v b/vlib/sync/pool/pool.v index 090c07b283..5d33f7730c 100644 --- a/vlib/sync/pool/pool.v +++ b/vlib/sync/pool/pool.v @@ -74,7 +74,7 @@ pub fn (mut pool PoolProcessor) set_max_jobs(njobs int) { // work_on_items returns *after* all threads finish. // You can optionally call get_results after that. pub fn (mut pool PoolProcessor) work_on_items(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) { diff --git a/vlib/term/ui/ui.v b/vlib/term/ui/ui.v index dd99a5bdbb..4a76212c9d 100644 --- a/vlib/term/ui/ui.v +++ b/vlib/term/ui/ui.v @@ -29,7 +29,7 @@ pub fn (mut ctx Context) write(s string) { if s == '' { return } - ctx.print_buf.push_many(s.str, s.len) + unsafe { ctx.print_buf.push_many(s.str, s.len) } } [inline]