Skip to content

Commit e909f77

Browse files
authored
m: Add leap_unwrap & make RawTask::TASK_LAYOUT non-optional
This PR makes RawTask::TASK_LAYOUT non-optional by panicking in const context in case of not having it. Note that due to MSRV being 1.47 a panic! could not be used in const context, so I resorted to out of bounds array read. The error message in case of failure is that of the faulty expression ('async_task_failed_to_compile[1]'), which is pretty bad, but in reality we should not be getting there ever.
1 parent 918ec72 commit e909f77

File tree

4 files changed

+19
-14
lines changed

4 files changed

+19
-14
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ jobs:
6868
matrix:
6969
# When updating this, the reminder to update the minimum supported
7070
# Rust version in Cargo.toml.
71-
rust: ['1.47']
71+
rust: ['1.57']
7272
steps:
7373
- uses: actions/checkout@v4
7474
- name: Install Rust

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ name = "async-task"
66
version = "4.5.0"
77
authors = ["Stjepan Glavina <[email protected]>"]
88
edition = "2018"
9-
rust-version = "1.47"
9+
rust-version = "1.57"
1010
license = "Apache-2.0 OR MIT"
1111
repository = "https://github.com/smol-rs/async-task"
1212
description = "Task abstraction for building executors"

src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,15 @@ macro_rules! leap {
9191
}};
9292
}
9393

94+
macro_rules! leap_unwrap {
95+
($x: expr) => {{
96+
match ($x) {
97+
Some(val) => val,
98+
None => panic!("called `Option::unwrap()` on a `None` value"),
99+
}
100+
}};
101+
}
102+
94103
mod header;
95104
mod raw;
96105
mod runnable;

src/raw.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub(crate) struct TaskVTable {
5252
/// debuggers to decode raw task memory blobs. Do not remove
5353
/// the field, even if it appears to be unused.
5454
#[allow(unused)]
55-
pub(crate) layout_info: &'static Option<TaskLayout>,
55+
pub(crate) layout_info: &'static TaskLayout,
5656
}
5757

5858
/// Memory layout of a task.
@@ -100,11 +100,11 @@ impl<F, T, S, M> Clone for RawTask<F, T, S, M> {
100100
}
101101

102102
impl<F, T, S, M> RawTask<F, T, S, M> {
103-
const TASK_LAYOUT: Option<TaskLayout> = Self::eval_task_layout();
103+
const TASK_LAYOUT: TaskLayout = Self::eval_task_layout();
104104

105105
/// Computes the memory layout for a task.
106106
#[inline]
107-
const fn eval_task_layout() -> Option<TaskLayout> {
107+
const fn eval_task_layout() -> TaskLayout {
108108
// Compute the layouts for `Header`, `S`, `F`, and `T`.
109109
let layout_header = Layout::new::<Header<M>>();
110110
let layout_s = Layout::new::<S>();
@@ -118,17 +118,17 @@ impl<F, T, S, M> RawTask<F, T, S, M> {
118118

119119
// Compute the layout for `Header` followed `S` and `union { F, T }`.
120120
let layout = layout_header;
121-
let (layout, offset_s) = leap!(layout.extend(layout_s));
122-
let (layout, offset_union) = leap!(layout.extend(layout_union));
121+
let (layout, offset_s) = leap_unwrap!(layout.extend(layout_s));
122+
let (layout, offset_union) = leap_unwrap!(layout.extend(layout_union));
123123
let offset_f = offset_union;
124124
let offset_r = offset_union;
125125

126-
Some(TaskLayout {
126+
TaskLayout {
127127
layout: unsafe { layout.into_std() },
128128
offset_s,
129129
offset_f,
130130
offset_r,
131-
})
131+
}
132132
}
133133
}
134134

@@ -227,12 +227,8 @@ where
227227
/// Returns the layout of the task.
228228
#[inline]
229229
fn task_layout() -> TaskLayout {
230-
match Self::TASK_LAYOUT {
231-
Some(tl) => tl,
232-
None => abort(),
233-
}
230+
Self::TASK_LAYOUT
234231
}
235-
236232
/// Wakes a waker.
237233
unsafe fn wake(ptr: *const ()) {
238234
// This is just an optimization. If the schedule function has captured variables, then

0 commit comments

Comments
 (0)