aboutsummaryrefslogtreecommitdiff
path: root/macros
diff options
context:
space:
mode:
authorGravatar Jorge Aparicio <jorge@japaric.io> 2017-12-09 17:14:51 +0100
committerGravatar Jorge Aparicio <jorge@japaric.io> 2017-12-09 17:17:56 +0100
commitd30bdcb096774c1f56d9823fb2fbb78bf5cd3584 (patch)
tree0a781ee30567a2f13de11d03f25736bf60d3866f /macros
parenta6dd004113fcbc03ffacacc519d742ee84886c1d (diff)
downloadrtic-d30bdcb096774c1f56d9823fb2fbb78bf5cd3584.tar.gz
rtic-d30bdcb096774c1f56d9823fb2fbb78bf5cd3584.tar.zst
rtic-d30bdcb096774c1f56d9823fb2fbb78bf5cd3584.zip
safe `&'static mut` references via init.resources
Diffstat (limited to 'macros')
-rw-r--r--macros/Cargo.toml3
-rw-r--r--macros/src/check.rs31
-rw-r--r--macros/src/trans.rs30
3 files changed, 54 insertions, 10 deletions
diff --git a/macros/Cargo.toml b/macros/Cargo.toml
index d51cbc26..d2e4da5b 100644
--- a/macros/Cargo.toml
+++ b/macros/Cargo.toml
@@ -12,7 +12,8 @@ version = "0.2.1"
[dependencies]
error-chain = "0.10.0"
quote = "0.3.15"
-rtfm-syntax = "0.2.0"
+# rtfm-syntax = "0.2.0"
+rtfm-syntax = { git = "https://github.com/japaric/rtfm-syntax", branch = "init-resources" }
syn = "0.11.11"
[lib]
diff --git a/macros/src/check.rs b/macros/src/check.rs
index 63cac1fa..f6fd9cc6 100644
--- a/macros/src/check.rs
+++ b/macros/src/check.rs
@@ -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;
}
diff --git a/macros/src/trans.rs b/macros/src/trans.rs
index 1008dfed..b540fd1d 100644
--- a/macros/src/trans.rs
+++ b/macros/src/trans.rs
@@ -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! {