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
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
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.
More Posts
onClick not staying with preact-render-to-string?Hello, I'm trying to make an SSR application by rendering components to strings. However, I need a bDeno project suddenly won't launch in VS CodeI haven't changed anything in my project or environment except having done a brew update/upgrade, whDeno BYOW help with macOS [Solved] and X11https://github.com/fazil47/deno_winit I'm trying to make Deno binding for winit for using WebGPU, iHow can I add a flag into Deno Deploy in production?Hi, I am wondering how do I add a flag in production on Deno deploy if I want to enable a certain flUploading Files to S3 Bucket usign deno-s3-lite-clientHello, I have been trying to make a file upload controller with deno's s3 lite client but I am facinDeno.cron errorHi I keep having an error on deno deploy where my cron job doesn't seem to execute well in productioHow to rewrite this: `import * as base32 from "jsr:@std/encoding@0.224.3/base32.ts";`I am migrating std from /x and I ran into this issue. What the correct way to import just base32.ts.Default export has no call signaturesI'm trying to import the default export from `npm:@feathersjs/express`, but Deno complains that the Catch/handle WebSocketStream connection reset errorHow/where to catch this error? I am using `WebSocketStream` and catching on `wss.opened` and on `wssdeno_kv_oauth + hono how to fetch data from google profile: state, plans, method names changing.In code examples i found in internet, used methods which already not the part of deno_kv_oauth. 1.