Rewrite your app in Rust, piece by piece, checked against the original at every step. Works with TypeScript, JavaScript, Python, Go, Ruby, Java, C, PHP, C#, Kotlin, PowerShell and Swift.
Rust agent-skillsclaude-codeclicode-migrationcodexffigolangjavajavascriptmcpmigrationnapiperformanceportingpyo3pythonrewriterubyrusttypescript
//! A PyO3 crate whose name is also the name of one of its exports (issue #1091).
//!
//! `#[pyfunction] fn slug` also defines a module called `slug`, so with the crate's library named
//! `slug` too, rustdoc's doctest build passes `--extern slug` and the unqualified `wrap_pyfunction!`
//! is ambiguous (`error[E0659]`). `cargo build` and `cargo test --lib` pass, which is why it only
//! shows up here.
mod common;
use std::path::{Path, PathBuf};
use std::process::Command;
use rust_anything::bridge::{self, Kind};
fn project(name: &str, files: &[(&str, &str)]) -> PathBuf {
let root = std::env::temp_dir().join(format!(
"rust-anything-pyo3-name-{name}-{}",
std::process::id()
));
let _ = std::fs::remove_dir_all(&root);
for (path, text) in files {
let path = root.join(path);
std::fs::create_dir_all(path.parent().unwrap()).unwrap();
std::fs::write(path, text).unwrap();
}
root
}
fn port(dir: &Path, text: &str) {
std::fs::write(dir.join("src/port.rs"), text).unwrap();
}
#[test]
fn a_crate_named_like_its_export_still_passes_cargo_test() {
let Some(_python) = common::python() else {
return;
};
let root = project(
"slug",
&[
(
"pyproject.toml",
"[project]\nname = \"slug\"\nversion = \"0.1.0\"\n",
),
(
"slug.py",
"def slug(s: str) -> str:\n return s.lower()\n",
),
],
);
let dir = root.join("rust/slug");
bridge::bridge(&bridge::Options {
root: &root,
unit: "slug.py",
kind: Some(Kind::Pyo3),
out: Some(dir.clone()),
name: Some("slug".to_string()),
force: false,
keep: Vec::new(),
lang: None,
batch: Vec::new(),
as_json: Vec::new(),
allocator: Default::default(),
})
.unwrap();
port(
&dir,
"pub fn slug(s: String) -> Result<String, String> {\n Ok(s.to_lowercase())\n}\n",
);
bridge::refresh(&dir).unwrap();
let lib = std::fs::read_to_string(dir.join("src/lib.rs")).unwrap();
assert!(
lib.contains("wrap_pyfunction!(crate::slug_py, m)"),
"the registration is qualified: {lib}"
);
// The check that fails without the qualification: rustdoc builds the doctest with `--extern slug`.
let test = Command::new("cargo")
.arg("test")
.current_dir(&dir)
.output()
.unwrap();
let out = format!(
"{}{}",
String::from_utf8_lossy(&test.stdout),
String::from_utf8_lossy(&test.stderr)
);
assert!(!out.contains("E0659"), "{out}");
assert!(test.status.success(), "{out}");
let _ = std::fs::remove_dir_all(&root);
}