Skip to content

Commit 9270af1

Browse files
bonziniBennoLossin
authored andcommitted
tests: ring_buf: remove dependency on allocator_api
Reuse examples/error.rs so that the error returned by EvenU64::new2() is not core::alloc::AllocError. This will allow running the test without the allocator_api feature. Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent fd63072 commit 9270af1

2 files changed

Lines changed: 17 additions & 16 deletions

File tree

examples/error.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
#![feature(allocator_api)]
1+
#![cfg_attr(feature = "alloc", feature(allocator_api))]
22

33
use core::convert::Infallible;
4+
5+
#[cfg(feature = "alloc")]
46
use std::alloc::AllocError;
57

68
#[derive(Debug)]
@@ -12,6 +14,7 @@ impl From<Infallible> for Error {
1214
}
1315
}
1416

17+
#[cfg(feature = "alloc")]
1518
impl From<AllocError> for Error {
1619
fn from(_: AllocError) -> Self {
1720
Self

tests/ring_buf.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#![feature(allocator_api)]
1+
#![cfg_attr(feature = "alloc", feature(allocator_api))]
2+
23
use core::{
3-
alloc::AllocError,
44
convert::Infallible,
55
marker::PhantomPinned,
66
mem::MaybeUninit,
@@ -14,6 +14,10 @@ use std::sync::Arc;
1414
mod mutex;
1515
use mutex::*;
1616

17+
#[path = "../examples/error.rs"]
18+
mod error;
19+
use error::Error;
20+
1721
#[pin_data(PinnedDrop)]
1822
pub struct RingBuffer<T, const SIZE: usize> {
1923
buffer: [MaybeUninit<T>; SIZE],
@@ -152,15 +156,15 @@ pub struct EvenU64 {
152156
}
153157

154158
impl EvenU64 {
155-
pub fn new2(value: u64) -> impl Init<Self, AllocError> {
159+
pub fn new2(value: u64) -> impl Init<Self, Error> {
156160
try_init!(Self {
157161
info: "Hello world!".to_owned(),
158162
data: if value % 2 == 0 {
159163
value
160164
} else {
161-
return Err(AllocError);
165+
return Err(Error);
162166
},
163-
}? AllocError)
167+
}? Error)
164168
}
165169
pub fn new(value: u64) -> impl Init<Self, ()> {
166170
try_init!(Self {
@@ -190,16 +194,10 @@ fn even_stack() {
190194

191195
#[test]
192196
fn even_failing() {
193-
assert!(matches!(
194-
Box::try_pin_init(EvenU64::new2(3)),
195-
Err(AllocError)
196-
));
197-
assert!(matches!(
198-
Arc::try_pin_init(EvenU64::new2(5)),
199-
Err(AllocError)
200-
));
201-
assert!(matches!(Box::try_init(EvenU64::new2(3)), Err(AllocError)));
202-
assert!(matches!(Arc::try_init(EvenU64::new2(5)), Err(AllocError)));
197+
assert!(matches!(Box::try_pin_init(EvenU64::new2(3)), Err(Error)));
198+
assert!(matches!(Arc::try_pin_init(EvenU64::new2(5)), Err(Error)));
199+
assert!(matches!(Box::try_init(EvenU64::new2(3)), Err(Error)));
200+
assert!(matches!(Arc::try_init(EvenU64::new2(5)), Err(Error)));
203201
}
204202

205203
#[test]

0 commit comments

Comments
 (0)