mirror of
https://github.com/rtic-rs/rtic.git
synced 2025-12-18 22:05:37 +01:00
safe &'static mut references via init.resources
This commit is contained in:
parent
a6dd004113
commit
d30bdcb096
6 changed files with 153 additions and 10 deletions
|
|
@ -77,7 +77,38 @@ pub fn app(app: check::App) -> Result<App> {
|
|||
}
|
||||
|
||||
fn resources(app: &App) -> Result<()> {
|
||||
for name in &app.init.resources {
|
||||
if let Some(resource) = app.resources.get(name) {
|
||||
ensure!(
|
||||
resource.expr.is_some(),
|
||||
"resource `{}`, allocated to `init`, must have an initial value",
|
||||
name
|
||||
);
|
||||
} else {
|
||||
bail!(
|
||||
"resource `{}`, allocated to `init`, must be a data resource",
|
||||
name
|
||||
);
|
||||
}
|
||||
|
||||
ensure!(
|
||||
!app.idle.resources.contains(name),
|
||||
"resources assigned to `init` can't be shared with `idle`"
|
||||
);
|
||||
|
||||
ensure!(
|
||||
app.tasks
|
||||
.iter()
|
||||
.all(|(_, task)| !task.resources.contains(name)),
|
||||
"resources assigned to `init` can't be shared with tasks"
|
||||
)
|
||||
}
|
||||
|
||||
for resource in app.resources.keys() {
|
||||
if app.init.resources.contains(resource) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if app.idle.resources.contains(resource) {
|
||||
continue;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -249,18 +249,30 @@ fn init(app: &App, main: &mut Vec<Tokens>, root: &mut Vec<Tokens>) {
|
|||
let mut rexprs = vec![];
|
||||
|
||||
for (name, resource) in init_resources {
|
||||
let _name = Ident::new(format!("_{}", name.as_ref()));
|
||||
lifetime = Some(quote!('a));
|
||||
|
||||
let ty = &resource.ty;
|
||||
|
||||
fields.push(quote! {
|
||||
pub #name: &'a mut #ty,
|
||||
});
|
||||
if app.init.resources.contains(name) {
|
||||
fields.push(quote! {
|
||||
pub #name: &'static mut #ty,
|
||||
});
|
||||
|
||||
rexprs.push(quote! {
|
||||
#name: &mut ::#_name,
|
||||
});
|
||||
let expr = &resource.expr;
|
||||
rexprs.push(quote!(#name: {
|
||||
static mut #name: #ty = #expr;
|
||||
&mut #name
|
||||
}));
|
||||
} else {
|
||||
let _name = Ident::new(format!("_{}", name.as_ref()));
|
||||
lifetime = Some(quote!('a));
|
||||
|
||||
fields.push(quote! {
|
||||
pub #name: &'a mut #ty,
|
||||
});
|
||||
|
||||
rexprs.push(quote! {
|
||||
#name: &mut ::#_name,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
root.push(quote! {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue