-
Notifications
You must be signed in to change notification settings - Fork 92
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
cached_result! doesn't work with async function #112
Comments
Hi @tsukit , the declarative macros don't support as many features as the proc_macros do. See the docs for proc macro examples https://docs.rs/cached/latest/cached/proc_macro/index.html, or the examples dir https://github.com/jaemk/cached/tree/master/examples. Your example would translate to
|
Thanks @jaemk . Unfortunately that doesn't really work in actual code that I have. The sample code I provided is a bit different from what I actually have where the returned object is a struct. When using the macro as you suggested, there are two major problems.
#[cached(result = true, time = 10, size = 10)]
async fn compute_thing(x: i32) -> std::result::Result<MyStruct, String> {
Ok(MyStruct { value: x * x })
}
struct MyStruct {
value: i32,
} This seems to be the same as reported here. What might I have missed? |
@tsukit To address number 2.) Making your type
For issue 1.), you can provide a code block that will be used to construct the type on first use. Using your example, this would look like the following:
|
Thanks! I am able to get it to work now with the suggestion. To clarify, is |
@jaemk I have a similar issue as above--my problem is that my code is in an I currently get the following error:
Current code within the
Thanks! |
Hi @glitchy , there's no reason why you can't use the use cached::proc_macro::cached;
use cached::stores::TimedCache;
use lazy_static::lazy_static;
struct Config {
lifespan: u64,
}
lazy_static! {
static ref CONFIG: Config = Config { lifespan: 10 };
}
// placeholder for the type you're adding an impl for
struct MyStruct {
value: i32,
}
// placeholder for `ClientWithMiddleware`
struct Client;
// placeholder for the `JwkSet` type
// return type needs to be Clone-able.
#[derive(Clone)]
struct Set(String);
impl MyStruct {
async fn jwks(&self, client: &Client, url: &str) -> std::result::Result<Set, ()> {
#[cached(
result = true,
type = r#"TimedCache<String, Set>"#,
create = r#"{ TimedCache::with_lifespan(CONFIG.lifespan) }"#,
convert = r#"{ url.to_string() }"#
)]
async fn build_and_cache(
client: &Client,
amount: i32,
url: &str,
) -> std::result::Result<Set, ()> {
// do stuff with client
Ok(Set(url.to_string()))
}
build_and_cache(client, self.value, url).await
}
}
#[tokio::test]
async fn test_cached() {
let s = MyStruct { value: 2 };
let c = Client;
s.jwks(&c, "https://place.it").await.unwrap();
} |
++1 @jaemk. i see now where i went astray--ty! |
Ok. The question has been addressed/answered. We can close this. Thanks @jaemk |
The following program fails to compile.
The error message is
It appears to be specific to async because removing
async
andawait
. It's like the macro for async case expands to invalid code.The text was updated successfully, but these errors were encountered: