623 Commits

Author SHA1 Message Date
Jorge Aparicio
e3e3bd42fd
applease rustfmt 2022-08-09 11:31:33 +02:00
Lachezar Lechev
5dc13b35c1
Vec - PartialEq where Rhs is the Vec 2022-07-17 11:18:07 +02:00
bors[bot]
1463915689
Merge #302
302: Add clarification in String r=japaric a=mriise

Needed to look into implementation to be sure String was allocating bytes not chars.

Co-authored-by: Melanie Riise <me@mriise.net>
2022-07-06 09:19:05 +00:00
Melanie Riise
4eed6ad9d7
Add clarification in String 2022-07-05 09:17:46 -07:00
Jorge Aparicio
610bf51238 v0.7.15 v0.7.15 2022-07-05 12:19:33 +02:00
bors[bot]
8c329c3292
Merge #301
301: Add some more Vec methods. r=japaric a=Dirbaio

Added

- `Vec::insert(index, element)`
- `Vec::remove(index)`
- `Vec::retain(f)`
- `Vec::retain_mut(f)`

Behavior matches `std` except `insert` which is now fallible and returns back the element when full. Implementation and docs taken from `std`.

Co-authored-by: Dario Nieuwenhuis <dirbaio@dirbaio.net>
2022-07-05 10:08:35 +00:00
Dario Nieuwenhuis
1d4c333ab6 Add changelog. 2022-07-04 19:13:02 +02:00
Dario Nieuwenhuis
2d953ef95a Vec: add retain, retain_mut. 2022-07-04 19:07:25 +02:00
Dario Nieuwenhuis
f32d5d9daa Vec: add remove. 2022-07-04 19:07:20 +02:00
Dario Nieuwenhuis
fedce67b58 Vec: add insert. 2022-07-04 19:07:10 +02:00
bors[bot]
344629e9ed
Merge #300
300: Fix OOB get_unchecked, shadow Vec::as_ptr methods r=japaric a=saethlin

The fixes in #280 missed one instance of UB. The get_unchecked_mut
inside VacantEntry::Insert can be out of bounds of the initialized
region of the backing Vec. When that happens, the call is UB. This is
detected both by the standard library's debug assertions which can be
enabled with -Zbuild-std and with Miri but only with
-Zmiri-tag-raw-pointers.

This also adds inherent as_ptr and as_mut_ptr methods to Vec which
shadow those provided by the Deref to a slice. Without this shadowing,
the change from get_unchecked_mut to as_mut_ptr.add wouldn't actually
fix the problem identified by the debug assertions or Miri, it just
hides it from the debug assertions. The core problem is that references
narrow provenance, so if we want to access outside of the initialized
region of a Vec we need to get a pointer to the array without passing
through a reference to the initialized region first. The pointers from
these shadowing methods can be used to access anywhere in the allocation,
whereas vec.as_slice().as_ptr() would be UB to use for access into the
uninitialized region.

Co-authored-by: Ben Kimock <kimockb@gmail.com>
2022-06-20 12:28:51 +00:00
Ben Kimock
270db441f7 Fix OOB get_unchecked, shadow Vec::as_ptr methods
The fixes in #280 missed one instance of UB. The get_unchecked_mut
inside VacantEntry::Insert can be out of bounds of the initialized
region of the backing Vec. When that happens, the call is UB. This is
detected both by the standard library's debug assertions which can be
enabled with -Zbuild-std and with Miri but only with
-Zmiri-tag-raw-pointers.

This also adds inherent as_ptr and as_mut_ptr methods to Vec which
shadow those provided by the Deref to a slice. Without this shadowing,
the change from get_unchecked_mut to as_mut_ptr.add wouldn't actually
fix the problem identified by the debug assertions or Miri, it just
hides it from the debug assertions. The core problem is that references
narrow provenance, so if we want to access outside of the initialized
region of a Vec we need to get a pointer to the array without passing
through a reference to the initialized region first. The pointers from
these shadowing methods can be used to access anywhere in the allocation,
whereas vec.as_slice().as_ptr() would be UB to use for access into the
uninitialized region.
2022-06-18 19:37:26 -04:00
Jorge Aparicio
309f150ca0 v0.6.14 v0.7.14 2022-06-15 15:11:11 +02:00
bors[bot]
84cedfd3d2
Merge #277
277: Add support for Arduino Uno and 2560 r=japaric a=mutantbob

This patch includes changes I needed to build my Arduino Uno sketches.  I incorporated the work from https://github.com/japaric/heapless/pull/264 to reduce the chance of conflicts.

Now that atomic-polyfill 0.1.8 supports AVR, things are a bit easier.


Co-authored-by: Robert Forsman <git@thoth.purplefrog.com>
2022-06-15 11:41:55 +00:00
bors[bot]
0a844a0204
Merge #298
298: compile-time check power of two invariant in Index{Set,Map}'s default method r=korken89 a=japaric

closes #295

I tested this manually with both `FnvIndexSet` and `FnvIndexMap`. I tried to add a compile fail test but discovered (the hard way) that all the const assert trickery in `sealed` is checked when the code is built with `cargo build` but *not* when the code is built with `cargo check`. As `trybuild` uses `cargo check` under the hood it's not possible to compile fail test any of those const asserts.

r? `@korken89` 

Co-authored-by: Jorge Aparicio <jorge.aparicio@ferrous-systems.com>
2022-06-15 11:28:46 +00:00
Jorge Aparicio
d58b8dbbb7 update changelog 2022-06-15 13:14:15 +02:00
bors[bot]
6dc013b7f9
Merge #299
299: remove the scoped_threads feature gate r=japaric a=japaric

the feature has been stabilized in a recent nightly

Co-authored-by: Jorge Aparicio <jorge.aparicio@ferrous-systems.com>
2022-06-15 11:12:58 +00:00
Jorge Aparicio
d9e620036b remove the scoped_threads feature gate
the feature has been stabilized in a recent nightly
2022-06-15 13:11:48 +02:00
Robert Forsman
178d44cc4f that cast was unnecessary 2022-06-14 13:50:34 -04:00
Robert Forsman
c8e6dcc3da act on japaric's requests using techniques from Rahix to more generically support AVR 2022-06-14 13:47:05 -04:00
Jorge Aparicio
1379a9859c compile-time check power of two invariant in Index{Set,Map}'s default method
closes #295
2022-06-14 12:30:52 +02:00
Robert Forsman
2150effc73 it seems that I do not need to emit cargo:rustc-cfg=has_atomics for avr-atmega* 2022-06-03 12:32:23 -04:00
Robert Forsman
3b9b9f7548 Merge remote-tracking branch 'upstream/master' 2022-05-28 18:38:23 -04:00
Jorge Aparicio
39273893de v0.7.13 v0.7.13 2022-05-16 12:14:22 +02:00
bors[bot]
511252b348
Merge #294
294: test BinaryHeap::into_vec r=japaric a=japaric



Co-authored-by: Jorge Aparicio <jorge.aparicio@ferrous-systems.com>
2022-05-16 10:03:46 +00:00
Jorge Aparicio
0917bcc7b3 test BinaryHeap::into_vec 2022-05-16 12:01:57 +02:00
bors[bot]
f07235e88e
Merge #262
262: add BinaryHeap::into_vec r=japaric a=Lambda-Logan

std::collections::BinaryHeap provides an into_vec method https://doc.rust-lang.org/std/collections/struct.BinaryHeap.html#method.into_vec

My use case is I want a sorted iterator  similar to BinaryHeap.into_sorted_iter. I found my self creating a new heapless:Vec from heapless::BinaryHeap::into_iter and then sorting it. It seemed better to return the underlying vec and sort that.

But the functionality seemed generally useful and helps the API jive a little better with std::collections::BinaryHeap.

Do you see any issues with this implementation? Also, apologies for anything weird... This is my first PR :) 



Co-authored-by: Logan Dimond <Lambda.Dimond@gmail.com>
Co-authored-by: Logan Dimond <lambda.dimond@gmail.com>
2022-05-16 09:19:30 +00:00
Logan Dimond
472a839f41 make 'into_vec' just using 'self.data' 2022-05-14 15:47:44 -04:00
Logan Dimond
338323b7f9
Merge branch 'japaric:master' into bheap_into_vec 2022-05-14 15:38:23 -04:00
Jorge Aparicio
4431ae1e25 v0.7.12 v0.7.12 2022-05-12 15:27:32 +02:00
bors[bot]
02773a5b07
Merge #293
293: unconditionally depend on atomic-polyfill for riscv32i target r=japaric a=japaric

due to a limitation in the llvm backend [1] the `riscv32i-unknown-none-elf` target lacks the `core::sync::atomic` API even though the actual hardware is capable of atomic loads and stores (`fence` instruction). thus, at this point in time, this target needs `atomic-polyfill` for a working SPSC implementation

[1]: https://github.com/knurling-rs/defmt/issues/597#issuecomment-934467023

fixes #271 
closes #272 
closes #273 

Co-authored-by: Jorge Aparicio <jorge.aparicio@ferrous-systems.com>
2022-05-12 13:06:27 +00:00
Jorge Aparicio
50c0930e1b update changelog 2022-05-12 15:04:06 +02:00
Jorge Aparicio
a94c1d0f59 riscv32imc needs the same treatment 2022-05-12 15:02:58 +02:00
Jorge Aparicio
34b05be696 add changelog entry 2022-05-12 14:58:29 +02:00
Jorge Aparicio
5d9e4b6268 unconditionally depend on atomic-polyfill for riscv32i target 2022-05-12 14:35:37 +02:00
Jorge Aparicio
3129e290a6 GHA: cargo check --no-default-features 2022-05-12 14:35:18 +02:00
bors[bot]
c2a022d9be
Merge #292
292: make Deque::pop_{front,back}_unchecked public r=japaric a=japaric

closes #268 

Co-authored-by: Jorge Aparicio <jorge.aparicio@ferrous-systems.com>
2022-05-12 11:07:44 +00:00
Jorge Aparicio
fbc5d5bf09 make Deque::pop_{front,back}_unchecked public 2022-05-12 13:06:50 +02:00
bors[bot]
622a67673d
Merge #291
291: add first/last API to IndexMap and IndexSet r=japaric a=japaric

closes #256 

Co-authored-by: Jorge Aparicio <jorge.aparicio@ferrous-systems.com>
2022-05-12 10:55:11 +00:00
Jorge Aparicio
7fd32f8a02 add first/last API to IndexMap and IndexSet 2022-05-12 12:54:15 +02:00
bors[bot]
e52d483271
Merge #290
290: optimize the codegen of Vec::clone r=japaric a=japaric

these changes optimize `Vec<u8, 1024>::clone` down to these operations

1. reserve the stack space (1028 bytes on 32-bit ARM) and leave it uninitialized
2. zero the `len` field
3. memcpy `len` bytes of data from the parent

analyzed source code

``` rust
use heapless::Vec;

fn clone(vec: &Vec<u8, 1024>) {
    let mut vec = vec.clone();
    black_box(&mut vec);
}

fn black_box<T>(val: &mut T) {
    unsafe { asm!("// {0}", in(reg) val) }
}
```

machine code with `lto = fat`, `codegen-units = 1` and `opt-level = 'z'` ('z' instead of 3 to avoid loop unrolling and keep the machine code readable)

``` armasm
00020100 <clone>:
   20100:              b5d0             push    {r4, r6, r7, lr}
   20102:              af02             add     r7, sp, #8
   20104:              f5ad 6d81        sub.w   sp, sp, #1032   ; 0x408
   20108:              2300             movs    r3, #0
   2010a:              c802             ldmia   r0!, {r1}
   2010c:              9301             str     r3, [sp, #4]
   2010e:              aa01             add     r2, sp, #4
   20110:       /--/-X b141             cbz     r1, 20124 <clone+0x24>
   20112:       |  |   4413             add     r3, r2
   20114:       |  |   f810 4b01        ldrb.w  r4, [r0], #1
   20118:       |  |   3901             subs    r1, #1
   2011a:       |  |   711c             strb    r4, [r3, #4]
   2011c:       |  |   9b01             ldr     r3, [sp, #4]
   2011e:       |  |   3301             adds    r3, #1
   20120:       |  |   9301             str     r3, [sp, #4]
   20122:       |  \-- e7f5             b.n     20110 <clone+0x10>
   20124:       \----> a801             add     r0, sp, #4
   20126:              f50d 6d81        add.w   sp, sp, #1032   ; 0x408
   2012a:              bdd0             pop     {r4, r6, r7, pc}
```

note that it's not optimizing step (3) to an actual `memcpy` because we lack the 'trait specialization' code that libstd uses

---

before `clone` was optimized to

1. reserve and zero (`memclr`) 1028 (!?) bytes of stack space
2. (unnecessarily) runtime check if `len` is equal or less than 1024 (capacity) -- this included a panicking branch
3. memcpy `len` bytes of data from the parent

Co-authored-by: Jorge Aparicio <jorge.aparicio@ferrous-systems.com>
2022-05-10 11:48:03 +00:00
Jorge Aparicio
b2268e7885 optimize the codegen of Vec::clone
these changes optimize `Vec<u8, 1024>::clone` down to these operations

1. reserve the stack space (1028 bytes on 32-bit ARM) and leave it uninitialized
2. zero the `len` field
3. memcpy `len` bytes of data from the parent

analyzed source code

``` rust
use heapless::Vec;

fn clone(vec: &Vec<u8, 1024>) {
    let mut vec = vec.clone();
    black_box(&mut vec);
}

fn black_box<T>(val: &mut T) {
    unsafe { asm!("// {0}", in(reg) val) }
}
```

machine code with `lto = fat`, `codegen-units = 1` and `opt-level = 'z'` ('z' instead of 3 to avoid loop unrolling and keep the machine code readable)

``` armasm
00020100 <clone>:
   20100:              b5d0             push    {r4, r6, r7, lr}
   20102:              af02             add     r7, sp, #8
   20104:              f5ad 6d81        sub.w   sp, sp, #1032   ; 0x408
   20108:              2300             movs    r3, #0
   2010a:              c802             ldmia   r0!, {r1}
   2010c:              9301             str     r3, [sp, #4]
   2010e:              aa01             add     r2, sp, #4
   20110:       /--/-X b141             cbz     r1, 20124 <clone+0x24>
   20112:       |  |   4413             add     r3, r2
   20114:       |  |   f810 4b01        ldrb.w  r4, [r0], #1
   20118:       |  |   3901             subs    r1, #1
   2011a:       |  |   711c             strb    r4, [r3, #4]
   2011c:       |  |   9b01             ldr     r3, [sp, #4]
   2011e:       |  |   3301             adds    r3, #1
   20120:       |  |   9301             str     r3, [sp, #4]
   20122:       |  \-- e7f5             b.n     20110 <clone+0x10>
   20124:       \----> a801             add     r0, sp, #4
   20126:              f50d 6d81        add.w   sp, sp, #1032   ; 0x408
   2012a:              bdd0             pop     {r4, r6, r7, pc}
```

note that it's not optimizing step (3) to an actual `memcpy` because we lack the 'trait specialization' code that libstd uses

---

before `clone` was optimized to

1. reserve and zero (`memclr`) 1028 (!?) bytes of stack space
2. (unnecessarily) runtime check if `len` is equal or less than 1024 (capacity) -- this included a panicking branch
3. memcpy `len` bytes of data from the parent
2022-05-10 12:49:02 +02:00
bors[bot]
aacc35997b
Merge #264
264: Add support for AVR. r=japaric a=jeremysalwen

In order to support AVR, the following changes were made:

 - AVR was added to the list of architectures not supporting atomics or cas.
 - AVR was configured to not use AtomicUsize, similarly to armv6m.
 - SortedLinkedList was modified to account for the fact that usize is differently sized on
   different architectures (16 bits on AVR).

Co-authored-by: Jeremy Salwen <jeremysalwen@gmail.com>
2022-05-10 09:34:40 +00:00
bors[bot]
705f93b400
Merge #289
289: entry API for IndexMap r=japaric a=japaric

rebased version of PR #276

`@MarcusGrass` thanks for the PR!

Co-authored-by: gramar <marcus.grass@gmail.com>
Co-authored-by: Jorge Aparicio <jorge.aparicio@ferrous-systems.com>
2022-05-09 15:00:27 +00:00
Jorge Aparicio
0484efb5bf update changelog 2022-05-09 16:58:51 +02:00
Jorge Aparicio
1735e6e58c rustfmt 2022-05-09 16:58:01 +02:00
gramar
b6f4bc03d2 Add changelog entry for Indexmap entry api 2022-05-09 16:57:47 +02:00
gramar
9b03b5940d Drive-by fix insert on full map 2022-05-09 16:57:03 +02:00
gramar
b6b3438f21 Add entry API 2022-05-09 16:55:35 +02:00
gramar
d2c85a18e0 Add entry API 2022-05-09 16:55:32 +02:00