A Frickn Chickn
A Frickn Chickn•5mo ago

How do you define an op in rust to return a string?

#[deno_core::op2(async)]
async fn op_fetch(#[string] req: String) -> String {
async { "hello".to_string() }.await
}
#[deno_core::op2(async)]
async fn op_fetch(#[string] req: String) -> String {
async { "hello".to_string() }.await
}
fails to compile
70 | #[deno_core::op2(async)]
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: message: Failed to parse #[op2]:
- Failed to parse signature
- Invalid return type
- Invalid return type
- Missing a #[string] attribute for type: String
70 | #[deno_core::op2(async)]
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: message: Failed to parse #[op2]:
- Failed to parse signature
- Invalid return type
- Invalid return type
- Missing a #[string] attribute for type: String
https://github.com/denoland/deno_core/blob/main/ops/op2/README.md#return-values this guide says I should mark string with #[string] String, however this also doesn't compile.
#[deno_core::op2(async)]
async fn op_fetch(#[string] req: String) -> #[string] String {
async { "hello".to_string() }.await
}
#[deno_core::op2(async)]
async fn op_fetch(#[string] req: String) -> #[string] String {
async { "hello".to_string() }.await
}
71 | async fn op_fetch2(#[string] req: String) -> #[string] String {
| ^ expected type
71 | async fn op_fetch2(#[string] req: String) -> #[string] String {
| ^ expected type
2 Replies
A Frickn Chickn
A Frickn Chickn•5mo ago
related: it'd be super nice if https://deno.com/blog/roll-your-own-javascript-runtime was updated to use #[op2] instead of the non-existent #[op] 😄 but that's not my immediate question I found answer in https://github.com/denoland/roll-your-own-javascript-runtime/blob/main/src/main.rs#L26-L31 Frustrating that the doc is out of date, but at least underlying repo has the updated info. Mentioning on the blog that it's using a deprecated/stale version of deno_core, and the repo has updated examples would be super helpful.
Deno AI Helper
Deno AI Helper•5mo ago
I wasn't able to find the answer to your question in the official Deno documentation. However, based on the error messages you've provided, it seems like there might be a misunderstanding in how to use the #[string] attribute with the return type in your Rust function. In the Deno documentation, the #[string] attribute is used to indicate that a parameter should be treated as a string when crossing the boundary between JavaScript and Rust. However, it's not used directly in the return type position. Instead of using #[string] in the return type, you should return a Result<String, AnyError> from your op function. Here's an example of how you might define an op that returns a string:
#[deno_core::op]
fn op_fetch(req: String) -> Result<String, deno_core::error::AnyError> {
Ok("hello".to_string())
}
#[deno_core::op]
fn op_fetch(req: String) -> Result<String, deno_core::error::AnyError> {
Ok("hello".to_string())
}
Please note that this advice is not based on the Deno documentation, and you should double-check the accuracy of the advice given. It's also possible that there have been updates or changes to the Deno core library that are not yet reflected in the documentation, so you might want to look at the latest examples in the Deno core repository or ask for help from the Deno community.