Don't rewrite it in Rust. Rewrite the 5% that's slow: profile your app, port the hot functions with your coding agent, prove they behave the same. Python, JS/TS, Ruby, Java, Kotlin, Go, PHP, C/C++, C#, R.
Rust agent-skillsai-agentsclaude-codeclaude-code-plugincodexdeveloper-toolsffimcpmcp-servernapi-rsnodejsoptimizationperformanceprofilerpyo3pythonrewrite-it-in-rustrubyrusttypescript
Merge #108: go functions returning a value and an error
EL4CTEOb301ca77d354
diff --cc src/golang.rsindex 777480f,ed78920..f30d6c4--- a/src/golang.rs+++ b/src/golang.rs@@@ -353,21 -348,17 +353,30 @@@ fn arg_list(hs: &Hotspot) -> Stringhs.params.iter().map(|p| p.name.as_str()).collect::<Vec<_>>().join(", ")}+/// The parameters passed on to another call: a variadic one is spread again with `...`.+fn call_args(hs: &Hotspot) -> String {+ hs.params+ .iter()+ .map(|p| if is_variadic(p) { format!("{}...", p.name) } else { p.name.clone() })+ .collect::<Vec<_>>()+ .join(", ")+}++fn is_variadic(p: &Param) -> bool {+ p.annotation.as_deref().is_some_and(|t| t.starts_with("..."))+}++ /// The function's value type, and whether it also returns an error: "(T, error)" (see describe.go).+ fn ret_parts(hs: &Hotspot) -> (String, bool) {+ let r = hs.returns.clone().unwrap_or_default();+ match r.strip_prefix('(').and_then(|t| t.strip_suffix(", error)")) {+ Some(t) => (t.to_string(), true),+ None => (r, false),+ }+ }+fn ret(hs: &Hotspot) -> String {- hs.returns.clone().unwrap_or_default()+ ret_parts(hs).0}pub fn record(ctx: &Ctx, go: &str, args: &[String], hs: &Hotspot, out: &Path, limit: usize) -> Result<Recording> {@@@ -379,18 -370,24 +388,27 @@@.params.iter().enumerate()- .map(|(i, p)| format!("a[{i}].({})", p.annotation.as_deref().unwrap_or("any")))+ .map(|(i, p)| match p.annotation.as_deref().and_then(|t| t.strip_prefix("...")) {+ Some(item) => format!("a[{i}].([]{item})..."),+ None => format!("a[{i}].({})", p.annotation.as_deref().unwrap_or("any")),+ }).collect();- let prologue = format!(- "if !fastAnythingRecInside {{\n\treturn fastAnythingRec([]any{{{args}}}, func(a []any) any {{ return {name}({casts}) }}).({ret})\n}}",- args = arg_list(hs),- name = hs.name,- casts = casts.join(", "),- ret = ret(hs),- );+ let (value, errors) = ret_parts(hs);+ let prologue = if errors {+ format!(+ "if !fastAnythingRecInside {{\n\tfaP := fastAnythingRec([]any{{{args}}}, func(a []any) any {{\n\t\tfaV, faErr := {name}({casts})\n\t\treturn fastAnythingRecPair{{faV, faErr}}\n\t}}).(fastAnythingRecPair)\n\tfaV, _ := faP.V.({value})\n\treturn faV, faP.Err\n}}",+ args = arg_list(hs),+ name = hs.name,+ casts = casts.join(", "),+ )+ } else {+ format!(+ "if !fastAnythingRecInside {{\n\treturn fastAnythingRec([]any{{{args}}}, func(a []any) any {{ return {name}({casts}) }}).({value})\n}}",+ args = arg_list(hs),+ name = hs.name,+ casts = casts.join(", "),+ )+ };let patched =native::insert_in_body(&text, "//", &format!("recording {}", hs.id), &prologue, find_body(&hs.name, hs.line))?;let rec = REC.replace("/*PKG*/", &pkg_name(ctx, hs)?).replace("/*ID*/", &hs.id);@@@ -424,9 -421,10 +442,13 @@@/// Rust type for a Go type.pub fn rust_type(t: &str) -> Option<String> {let t = t.trim();+ if let Some(item) = t.strip_prefix("...") {+ return Some(format!("Vec<{}>", rust_type(item)?));+ }+ // (T, error): the port returns Result<T, String>, Err for the error+ if let Some(value) = t.strip_prefix('(').and_then(|t| t.strip_suffix(", error)")) {+ return rust_type(value);+ }Some(match t {"int" | "int64" => "i64".into(),"int32" | "rune" => "i32".into(),@@@ -460,8 -458,8 +482,9 @@@ pub const FLAVOR: Flavor = Flavor"Go strings are UTF-8 bytes: `len(s)` and `s[i]` are bytes, `for _, r := range s` and `[]rune(s)` are characters. Do the same in Rust.","A nil slice or map arrives (and may be returned) as an empty Vec or HashMap: fast-anything treats them as equal.","Map iteration order is random in Go: return results in the order the original does when it's deterministic.",+ "A function returning (T, error): return `Err(message)` where it returns a non-nil error (its message is `err.Error()`); enabled, Go gets `errors.New(message)` back, so callers comparing against sentinel errors (`err == io.EOF`, `errors.Is`) won't match.",],+ floats: None,};pub fn scaffold(ctx: &Ctx, hs: &Hotspot, helpers: &[Hotspot], port: &Port) -> Result<String> {@@@ -515,9 -513,14 +538,14 @@@ pub fn enable(ctx: &Ctx, port: &Port) -.replace("/*PARAMS*/", ¶ms.join(", ")).replace("/*ARGS*/", &arg_list(hs)).replace("/*RET*/", &ret(hs))+ .replace("/*ERRORS*/", if ret_parts(hs).1 { "true" } else { "false" }).replace("/*LIB*/", &lib)};- let prologue = format!("if faR, faOK := faFast_{s}({}); faOK {{\n\treturn faR\n}}", call_args(hs));+ let prologue = if ret_parts(hs).1 {- format!("if faR, faErr, faOK := faFast_{s}({}); faOK {{\n\treturn faR, faErr\n}}", arg_list(hs))++ format!("if faR, faErr, faOK := faFast_{s}({}); faOK {{\n\treturn faR, faErr\n}}", call_args(hs))+ } else {- format!("if faR, _, faOK := faFast_{s}({}); faOK {{\n\treturn faR\n}}", arg_list(hs))++ format!("if faR, _, faOK := faFast_{s}({}); faOK {{\n\treturn faR\n}}", call_args(hs))+ };let patched = native::insert_in_body(&text, "//", &port.id, &prologue, find_body(&port.func, hs.line))?;for (path, template) in bridge_files(&file, port)?.iter().zip([BRIDGE, BRIDGE_WINDOWS, BRIDGE_UNIX, BRIDGE_NOCGO]) {fs::write(path, fill(template))?;@@@ -541,79 -544,100 +569,173 @@@ pub fn disable(ctx: &Ctx, port: &Port)mod tests {use super::*;+ #[test]+ fn the_bridge_returns_the_ports_error() {+ if !Command::new("go").arg("version").output().is_ok_and(|o| o.status.success()) {+ eprintln!("no go on PATH, skipping");+ return;+ }+ let dir = std::env::temp_dir().join(format!("fa-go-bridge-error-test-{}", std::process::id()));+ fs::create_dir_all(&dir).unwrap();+ let bridge = BRIDGE+ .replace("/*S*/", "t")+ .replace("/*PKG*/", "main")+ .replace("/*ID*/", "m.parse")+ .replace("/*PARAMS*/", "s string")+ .replace("/*ARGS*/", "s")+ .replace("/*RET*/", "int")+ .replace("/*ERRORS*/", "true")+ .replace("/*LIB*/", "\"lib\"");+ // a stand-in for the port: an error for "x", a number otherwise+ let main = r#"package main++ import "fmt"++ func faLoad_t(path string) bool { return true }++ func faInvoke_t(in []byte) []byte {+ if string(in) == `["x"]` {+ return []byte(`{"error":"bad input"}`)+ }+ return []byte(`{"ok":7}`)+ }++ func main() {+ n, err, ok := faFast_t("x")+ fmt.Println(n, err, ok)+ n, err, ok = faFast_t("7")+ fmt.Println(n, err, ok)+ }+ "#;+ fs::write(dir.join("go.mod"), "module t\n\ngo 1.22\n").unwrap();+ fs::write(dir.join("bridge.go"), bridge).unwrap();+ fs::write(dir.join("main.go"), main).unwrap();+ let run = Command::new("go").args(["run", "."]).current_dir(&dir).env("CGO_ENABLED", "0").output().unwrap();+ let _ = fs::remove_dir_all(&dir);+ assert!(run.status.success(), "{}", String::from_utf8_lossy(&run.stderr));+ assert_eq!(String::from_utf8_lossy(&run.stdout), "0 bad input true\n7 <nil> true\n");+ }++ #[test]+ fn a_value_and_an_error_are_portable_and_recorded() {+ if !Command::new("go").arg("version").output().is_ok_and(|o| o.status.success()) {+ eprintln!("no go on PATH, skipping");+ return;+ }+ let dir = std::env::temp_dir().join(format!("fa-go-value-error-test-{}", std::process::id()));+ fs::create_dir_all(&dir).unwrap();+ fs::write(dir.join("go.mod"), "module demo\n\ngo 1.22\n").unwrap();+ let lib = "package main\n\nimport \"strconv\"\n\nfunc parse(s string) (int, error) {\n\treturn strconv.Atoi(s)\n}\n\nfunc half(n int) (h int, err error) {\n\th = n / 2\n\treturn\n}\n\nfunc split(s string) (int, int) {\n\treturn len(s), 0\n}\n";+ let main = "package main\n\nimport \"fmt\"\n\nfunc main() {\n\tt := 0\n\tfor _, s := range []string{\"1\", \"22\", \"x\", \"333\"} {\n\t\tn, err := parse(s)\n\t\tif err == nil {\n\t\t\tt += n\n\t\t}\n\t}\n\th, _ := half(t)\n\ta, _ := split(\"ab\")\n\tfmt.Println(t, h, a)\n}\n";+ fs::write(dir.join("lib.go"), lib).unwrap();+ fs::write(dir.join("main.go"), main).unwrap();+ let ctx = Ctx::open(&dir).unwrap();+ let funcs = describe(&ctx, "go").unwrap();+ let find = |n: &str| funcs.iter().find(|f| f.name == n).unwrap();+ assert_eq!(find("parse").returns.as_deref(), Some("(int, error)"));+ assert!(find("parse").blockers.is_empty(), "{:?}", find("parse").blockers);+ assert_eq!(find("half").returns.as_deref(), Some("(int, error)"));+ assert!(find("half").blockers.is_empty(), "named results: {:?}", find("half").blockers);+ assert!(find("split").blockers.iter().any(|b| b.starts_with("returns more than one value")));+ assert_eq!(rust_type("(int, error)").as_deref(), Some("i64"));+ let hs = Hotspot {+ id: "main.parse".into(),+ file: "lib.go".into(),+ line: 5,+ name: "parse".into(),+ qualname: "parse".into(),+ params: vec![Param { name: "s".into(), annotation: Some("string".into()), ..Param::default() }],+ returns: Some("(int, error)".into()),+ ..Hotspot::default()+ };+ let out = dir.join("rec.jsonl");+ let rec = record(&ctx, "go", &["run".into(), ".".into()], &hs, &out, 50);+ let lines = fs::read_to_string(&out).unwrap_or_default();+ let after = fs::read_to_string(dir.join("lib.go")).unwrap();+ let _ = fs::remove_dir_all(&dir);+ assert_eq!(rec.unwrap().calls, 4);+ assert!(lines.contains(r#"{"args":["22"],"result":22}"#), "{lines}");+ assert!(lines.contains(r#"{"args":["x"],"raises":"strconv.Atoi: parsing \"x\": invalid syntax"}"#), "{lines}");+ assert!(+ lines.contains(r#""raises":"strconv.Atoi: parsing \"\": invalid syntax","derived":"edge"}"#),+ "{lines}"+ );+ assert_eq!(after, lib);+ }++ #[test]+ fn variadic_functions_are_portable() {+ if !Command::new("go").arg("version").output().is_ok_and(|o| o.status.success()) {+ eprintln!("no go on PATH, skipping");+ return;+ }+ let dir = std::env::temp_dir().join(format!("fa-go-variadic-test-{}", std::process::id()));+ fs::create_dir_all(&dir).unwrap();+ fs::write(dir.join("go.mod"), "module demo\n\ngo 1.22\n").unwrap();+ let lib = "package main\n\nfunc total(scale int, xs ...int) int {\n\ts := 0\n\tfor _, x := range xs {\n\t\ts += x * scale\n\t}\n\treturn s\n}\n";+ let main = "package main\n\nimport \"fmt\"\n\nfunc main() {\n\tfmt.Println(total(2, 1, 2, 3), total(3), total(1, []int{4, 5}...))\n}\n";+ fs::write(dir.join("lib.go"), lib).unwrap();+ fs::write(dir.join("main.go"), main).unwrap();+ let ctx = Ctx::open(&dir).unwrap();+ let f = describe(&ctx, "go").unwrap().into_iter().find(|f| f.name == "total").unwrap();+ assert!(f.blockers.is_empty(), "{:?}", f.blockers);+ assert_eq!(rust_type(f.params[1].annotation.as_deref().unwrap()).as_deref(), Some("Vec<i64>"));+ let hs = Hotspot {+ id: "main.total".into(),+ file: "lib.go".into(),+ line: 3,+ name: "total".into(),+ qualname: "total".into(),+ params: f.params.clone(),+ returns: f.returns.clone(),+ ..Hotspot::default()+ };+ let out = dir.join("rec.jsonl");+ let rec = record(&ctx, "go", &["run".into(), ".".into()], &hs, &out, 50);+ let lines = fs::read_to_string(&out).unwrap_or_default();+ let after = fs::read_to_string(dir.join("lib.go")).unwrap();+ let _ = fs::remove_dir_all(&dir);+ assert_eq!(rec.unwrap().calls, 3);+ assert!(lines.contains(r#"{"args":[2,[1,2,3]],"result":12}"#), "{lines}");+ assert!(+ lines.contains(r#"{"args":[3,null],"result":0}"#) || lines.contains(r#"{"args":[3,[]],"result":0}"#),+ "{lines}"+ );+ assert_eq!(after, lib);+ }++ #[test]+ fn records_a_function_that_lives_next_to_main() {+ if !Command::new("go").arg("version").output().is_ok_and(|o| o.status.success()) {+ eprintln!("no go on PATH, skipping");+ return;+ }+ let dir = std::env::temp_dir().join(format!("fa-go-main-file-test-{}", std::process::id()));+ fs::create_dir_all(&dir).unwrap();+ fs::write(dir.join("go.mod"), "module demo\n\ngo 1.22\n").unwrap();+ let main = "package main\n\nimport \"fmt\"\n\nfunc double(x int) int {\n\treturn 2 * x\n}\n\nfunc main() {\n\tt := 0\n\tfor i := 0; i < 7; i++ {\n\t\tt += double(i)\n\t}\n\tfmt.Println(t)\n}\n";+ fs::write(dir.join("main.go"), main).unwrap();+ let ctx = Ctx::open(&dir).unwrap();+ let hs = Hotspot {+ id: "main.double".into(),+ file: "main.go".into(),+ line: 5,+ name: "double".into(),+ qualname: "double".into(),+ params: vec![Param { name: "x".into(), annotation: Some("int".into()), ..Param::default() }],+ returns: Some("int".into()),+ ..Hotspot::default()+ };+ let out = dir.join("rec.jsonl");+ let rec = record(&ctx, "go", &["run".into(), ".".into()], &hs, &out, 50);+ let after = fs::read_to_string(dir.join("main.go")).unwrap();+ let lines = fs::read_to_string(&out).unwrap_or_default();+ let _ = fs::remove_dir_all(&dir);+ assert_eq!(rec.unwrap().calls, 7);+ assert!(lines.contains("{\"args\":[3],\"result\":6}"), "{lines}");+ assert_eq!(after, main, "the app is left as it was");+ }+#[test]fn go_commands_and_profiles() {let s = |v: &[&str]| v.iter().map(|x| x.to_string()).collect::<Vec<_>>();