mirror of
https://github.com/rust-embedded/heapless.git
synced 2025-10-02 14:54:30 +00:00
Merge branch 'master' into master
This commit is contained in:
commit
6658509412
@ -182,7 +182,7 @@ where
|
|||||||
Ok(values)
|
Ok(values)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
deserializer.deserialize_seq(ValueVisitor(PhantomData))
|
deserializer.deserialize_map(ValueVisitor(PhantomData))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -225,7 +225,7 @@ where
|
|||||||
Ok(values)
|
Ok(values)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
deserializer.deserialize_seq(ValueVisitor(PhantomData))
|
deserializer.deserialize_map(ValueVisitor(PhantomData))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
15
src/mpmc.rs
15
src/mpmc.rs
@ -493,10 +493,14 @@ unsafe fn dequeue<T>(buffer: *mut Cell<T>, dequeue_pos: &AtomicU8, mask: u8) ->
|
|||||||
}
|
}
|
||||||
} else if dif < 0 {
|
} else if dif < 0 {
|
||||||
return None;
|
return None;
|
||||||
|
} else {
|
||||||
|
if pos == 255 && dif == 255{
|
||||||
|
return None;
|
||||||
} else {
|
} else {
|
||||||
pos = dequeue_pos.load(Ordering::Relaxed);
|
pos = dequeue_pos.load(Ordering::Relaxed);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let data = (*cell).data.as_ptr().read();
|
let data = (*cell).data.as_ptr().read();
|
||||||
(*cell)
|
(*cell)
|
||||||
@ -560,4 +564,15 @@ mod tests {
|
|||||||
assert_eq!(q.dequeue(), Some(1));
|
assert_eq!(q.dequeue(), Some(1));
|
||||||
assert_eq!(q.dequeue(), None);
|
assert_eq!(q.dequeue(), None);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn blocking(){
|
||||||
|
let q = Q2::new();
|
||||||
|
for _ in 0..255 {
|
||||||
|
q.enqueue(0).unwrap();
|
||||||
|
q.dequeue();
|
||||||
|
}
|
||||||
|
// this should not block forever
|
||||||
|
q.dequeue();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,6 +21,9 @@ unsafe impl XCore for MultiCore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub unsafe trait Uxx: Into<usize> + Send {
|
pub unsafe trait Uxx: Into<usize> + Send {
|
||||||
|
#[doc(hidden)]
|
||||||
|
fn saturate(x: usize) -> Self;
|
||||||
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
fn truncate(x: usize) -> Self;
|
fn truncate(x: usize) -> Self;
|
||||||
|
|
||||||
@ -39,15 +42,19 @@ pub unsafe trait Uxx: Into<usize> + Send {
|
|||||||
}
|
}
|
||||||
|
|
||||||
unsafe impl Uxx for u8 {
|
unsafe impl Uxx for u8 {
|
||||||
fn truncate(x: usize) -> Self {
|
fn saturate(x: usize) -> Self {
|
||||||
let max = ::core::u8::MAX;
|
let max = Self::max_value() as usize;
|
||||||
if x >= usize::from(max) {
|
if x >= usize::from(max) {
|
||||||
max
|
max as Self
|
||||||
} else {
|
} else {
|
||||||
x as u8
|
x as Self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn truncate(x: usize) -> Self {
|
||||||
|
x as Self
|
||||||
|
}
|
||||||
|
|
||||||
unsafe fn load_acquire<C>(x: *const Self) -> Self
|
unsafe fn load_acquire<C>(x: *const Self) -> Self
|
||||||
where
|
where
|
||||||
C: XCore,
|
C: XCore,
|
||||||
@ -79,15 +86,19 @@ unsafe impl Uxx for u8 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
unsafe impl Uxx for u16 {
|
unsafe impl Uxx for u16 {
|
||||||
fn truncate(x: usize) -> Self {
|
fn saturate(x: usize) -> Self {
|
||||||
let max = ::core::u16::MAX;
|
let max = Self::max_value() as usize;
|
||||||
if x >= usize::from(max) {
|
if x >= usize::from(max) {
|
||||||
max
|
max as Self
|
||||||
} else {
|
} else {
|
||||||
x as u16
|
x as Self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn truncate(x: usize) -> Self {
|
||||||
|
x as Self
|
||||||
|
}
|
||||||
|
|
||||||
unsafe fn load_acquire<C>(x: *const Self) -> Self
|
unsafe fn load_acquire<C>(x: *const Self) -> Self
|
||||||
where
|
where
|
||||||
C: XCore,
|
C: XCore,
|
||||||
@ -119,6 +130,10 @@ unsafe impl Uxx for u16 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
unsafe impl Uxx for usize {
|
unsafe impl Uxx for usize {
|
||||||
|
fn saturate(x: usize) -> Self {
|
||||||
|
x
|
||||||
|
}
|
||||||
|
|
||||||
fn truncate(x: usize) -> Self {
|
fn truncate(x: usize) -> Self {
|
||||||
x
|
x
|
||||||
}
|
}
|
||||||
|
@ -174,7 +174,7 @@ where
|
|||||||
{
|
{
|
||||||
/// Returns the maximum number of elements the queue can hold
|
/// Returns the maximum number of elements the queue can hold
|
||||||
pub fn capacity(&self) -> U {
|
pub fn capacity(&self) -> U {
|
||||||
U::truncate(N::to_usize())
|
U::saturate(N::to_usize())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns `true` if the queue has a length of 0
|
/// Returns `true` if the queue has a length of 0
|
||||||
@ -205,7 +205,7 @@ where
|
|||||||
let head = self.0.head.load_relaxed().into();
|
let head = self.0.head.load_relaxed().into();
|
||||||
let tail = self.0.tail.load_relaxed().into();
|
let tail = self.0.tail.load_relaxed().into();
|
||||||
|
|
||||||
tail.wrapping_sub(head)
|
U::truncate(tail.wrapping_sub(head)).into()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -732,6 +732,20 @@ mod tests {
|
|||||||
assert_eq!(items.next_back(), None);
|
assert_eq!(items.next_back(), None);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn iter_overflow() {
|
||||||
|
let mut rb: Queue<i32, U4, u8> = Queue::u8();
|
||||||
|
|
||||||
|
rb.enqueue(0).unwrap();
|
||||||
|
for _ in 0..300 {
|
||||||
|
let mut items = rb.iter_mut();
|
||||||
|
assert_eq!(items.next(), Some(&mut 0));
|
||||||
|
assert_eq!(items.next(), None);
|
||||||
|
rb.dequeue().unwrap();
|
||||||
|
rb.enqueue(0).unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn iter_mut() {
|
fn iter_mut() {
|
||||||
let mut rb: Queue<i32, U4> = Queue::new();
|
let mut rb: Queue<i32, U4> = Queue::new();
|
||||||
@ -764,6 +778,7 @@ mod tests {
|
|||||||
assert_eq!(items.next(), None);
|
assert_eq!(items.next(), None);
|
||||||
assert_eq!(items.next_back(), None);
|
assert_eq!(items.next_back(), None);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn sanity() {
|
fn sanity() {
|
||||||
let mut rb: Queue<i32, U4> = Queue::new();
|
let mut rb: Queue<i32, U4> = Queue::new();
|
||||||
|
@ -96,7 +96,7 @@ where
|
|||||||
/// assert!(String::from_utf8(v).is_err());
|
/// assert!(String::from_utf8(v).is_err());
|
||||||
/// ```
|
/// ```
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn from_utf8(vec: Vec<u8, N>) -> Result<(String<N>), Utf8Error> {
|
pub fn from_utf8(vec: Vec<u8, N>) -> Result<String<N>, Utf8Error> {
|
||||||
// validate input
|
// validate input
|
||||||
str::from_utf8(&*vec)?;
|
str::from_utf8(&*vec)?;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user