Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion codegen/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,7 @@ fn schema_source(
// The framings, format and handshake a body names by mark and no import
// above covers.
let extra: String = [
("::core::future", "Future"),
("comline_runtime::contract", "Datagram3Framing"),
("comline_runtime::format", "Json"),
("comline_runtime::framing", "JsonRpcFraming"),
Expand Down Expand Up @@ -2418,7 +2419,7 @@ fn protocol(
format!("$Result<{}, $Fault<{}>>", f.ret, f.err_ty)
};
s.push_str(&format!(
" fn {}(&self{args}) -> impl ::core::future::Future<Output = {output}> + Send;\n",
" fn {}(&self{args}) -> impl $Future<Output = {output}> + Send;\n",
f.ident
));
} else if f.one_way {
Expand Down
1 change: 1 addition & 0 deletions codegen/src/names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const PATHS: &[(&str, &str)] = &[
("Vec", "::std::vec::Vec"),
("String", "::std::string::String"),
("Box", "::std::boxed::Box"),
("Future", "::core::future::Future"),
("Serialize", "::serde::Serialize"),
("Deserialize", "::serde::Deserialize"),
("Client", "::comline_runtime::client::Client"),
Expand Down
2 changes: 2 additions & 0 deletions codegen/tests/compiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1924,6 +1924,7 @@ fn colliding_schemas() -> Vec<(String, Vec<FrozenUnit>)> {
"Vec",
"Result",
"Box",
"Future",
"Serialize",
"Deserialize",
"Client",
Expand Down Expand Up @@ -2699,6 +2700,7 @@ fn protocols_named_like_the_package_modules_types_build() {
"Comline2Handshake",
"Unavailable",
"Result",
"Future",
"Services",
];
let shared = std::env::temp_dir().join(format!("comline-rust-names-{}", std::process::id()));
Expand Down
55 changes: 51 additions & 4 deletions codegen/tests/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1982,10 +1982,9 @@ fn async_makes_a_connection_protocol_async() {
src.contains("pub trait Rpc: Send + Sync + 'static {"),
"{src}"
);
assert!(
src.contains("-> impl ::core::future::Future<Output = "),
"{src}"
);
assert!(src.contains("-> impl Future<Output = "), "{src}");
assert!(src.contains("use ::core::future::Future;\n"), "{src}");
assert!(!src.contains("::core::future::Future<"), "{src}");
assert!(src.contains("peer: &impl AsyncPeer"), "{src}");
assert!(
src.contains("impl<S: Rpc> AsyncDispatch for RpcAsyncDispatcher<S> {"),
Expand Down Expand Up @@ -2030,6 +2029,54 @@ fn async_makes_a_connection_protocol_async() {
assert!(!src.contains("Async"), "{src}");
}

/// The async handlers' `impl Future` is written by its imported name, which
/// is only imported where it is used; a schema that declares a `Future` of its
/// own keeps the full path and imports nothing.
#[test]
fn the_future_import_is_only_where_it_is_used_and_free() {
let one = |async_api: bool, declare: bool| {
let mut units = one_plain_protocol("Rpc");
if declare {
units.push(FrozenUnit::Struct {
docstring: None,
parameters: vec![],
name: "Future".into(),
fields: vec![],
span: (0, 0),
});
}
let schemas = vec![("rpc".to_string(), units)];
let req = GenerateRequest {
async_api,
..code_req(&schemas)
};
generate_rust(&req).unwrap().remove(0).contents
};

let blocking = one(false, false);
assert!(!blocking.contains("Future"), "{blocking}");

let asynchronous = one(true, false);
assert_eq!(
asynchronous.matches("use ::core::future::Future;").count(),
1
);
assert!(
asynchronous.contains("-> impl Future<Output = "),
"{asynchronous}"
);

let declared = one(true, true);
assert!(
!declared.contains("use ::core::future::Future;"),
"{declared}"
);
assert!(
declared.contains("-> impl ::core::future::Future<Output = "),
"{declared}"
);
}

#[test]
fn an_async_protocols_names_are_checked_for_collisions() {
let mut units = one_plain_protocol("Rpc");
Expand Down
Loading