Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[UR] local storage inside function #11726

Closed
nicolasboulay opened this issue Jan 22, 2014 · 8 comments
Closed

[UR] local storage inside function #11726

nicolasboulay opened this issue Jan 22, 2014 · 8 comments
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@nicolasboulay
Copy link

It could be great to have local storage that are unique by function call site (like Lustre does).

This reduce the need for dynamique memory and it's much easier than doing it by hand. This is also great for real time application, or application that refuse dynamic memory (for security, safety or speed reason).

@pnkfelix
Copy link
Member

have you considered expressing this via a macro?

@nicolasboulay
Copy link
Author

No, i'm new to rust. Maybe if the macros control a call site, this could be possible to allocate a memory at the beginning and use it after.

Macro looks complex, a little bit as the template in C++. Is it a new langage or an extension of normal rust langage to be executed at compile time ?

@pnkfelix
Copy link
Member

@nicolasboulay my thinking was that the function call sites would use be a macro invocation that would expand into the code for allocating and passing along the state you want.

But this may not be as easy as I originally thought, since I hadn't considered how to allocate the per-call-site storage. (Also, I'm not familiar with Lustre, so I might be misinterpreting what your feature request actually is. You may want to consider providing a bit more information, i.e. a concrete example of a piece of code showing how you'd be forced to write X today, and how you would write X after adding this feature.)

As for your question, the macro system is still a volatile feature of rust. There is macro_rules! for doing macro-by-example that is sort of like Scheme syntax-rules style macros. There is also a procedural macro system that landed recently (#11151) that I am less familiar with.

@alexcrichton
Copy link
Member

I think that we have this today in the form of local_data and its related macros:

use std::local_data;
fn foo() {
    local_data_key!(foo: uint);
    local_data::get(foo, |val| { ... });
    local_data::set(foo, 4);
}

And the key foo is scoped to just the function that it was defined in.

I'm closing for now, but please feel free to reopen if I misunderstand!

@zkamsler
Copy link
Contributor

I have done a little reading on Lustre, which is a dataflow language. It does not have functions as such, but rather 'nodes' that can store some local variables as well as the previous values of parameters and outputs. Data is not local to the call site as such, but rather to the node in the graph. (http://rw4.cs.uni-saarland.de/teaching/esd07/papers/lustre.pdf)

As such, it should be relatively straightforward (if tedious) to achieve a similar effect by making a struct with the desired data, and making a method on that. As long as there are no cycles, calls to other nodes can be represented by nesting structs.

@alexcrichton, it is not what you thought it was, but I am not sure it is anything that needs to be specifically added to Rust.

@alexcrichton
Copy link
Member

Sounds like I shouldn't have closed it!

@alexcrichton alexcrichton reopened this Jan 22, 2014
@nicolasboulay
Copy link
Author

For example in signal processing, pid controller is quite common code : ( https://en.wikipedia.org/wiki/PID_controller )

double pid(double e, double P, double I, double D){
static double i, past_error;
err = e-s;
i += err;
d = error - past_error;
past_error = error;
return P_e+I_i+D*d;
} //P I D are fix parameter

It's not possible to reuse this code. Typical code will need to allocate "i" and "past_error" "somewhere"', before the call.

double pid(double e, double P, double I, double D, double * I, double * past_error){
err = e-s;
(_i) += err;
d = error - (_past_error);
(past_error) = error;
return P_e+I
(_i)+D*d;
}

The code is more complexe, it add some data management in some init fonction.

@steveklabnik
Copy link
Member

I'm pulling a massive triage effort to get us ready for 1.0. As part of this, I'm moving stuff that's wishlist-like to the RFCs repo, as that's where major new things should get discussed/prioritized.

This issue has been moved to the RFCs repo: rust-lang/rfcs#795

flip1995 pushed a commit to flip1995/rust that referenced this issue Nov 2, 2023
Expand docs on clippy::todo

https://doc.rust-lang.org/nightly/core/macro.todo.html describes that `todo!()` is intended for explicitly unfinished code. Explain this, and mention `unimplemented!()` as an alternative.

Whilst we're here, improve the punctuation on the other lints.

changelog: [`todo`]: expand docs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one.
Projects
None yet
Development

No branches or pull requests

6 participants