Skip to content

Commit 9b27364

Browse files
Split 'return_task' function (#3914)
1 parent e4ad4f9 commit 9b27364

File tree

3 files changed

+32
-60
lines changed

3 files changed

+32
-60
lines changed

tools/slicec-cs/src/generators/dispatch_generator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ request.DecodeArgsAsync(
312312
fn operation_declaration(operation: &Operation) -> CodeBlock {
313313
let mut builder = FunctionBuilder::new(
314314
"public",
315-
&operation.return_task(true),
315+
&operation.dispatch_return_task(),
316316
&operation.escape_identifier_with_suffix("Async"),
317317
FunctionType::Declaration,
318318
);

tools/slicec-cs/src/generators/proxy_generator.rs

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ fn proxy_operation_impl(operation: &Operation) -> CodeBlock {
275275
let namespace = &operation.namespace();
276276
let operation_name = operation.escape_identifier();
277277
let async_operation_name = operation.escape_identifier_with_suffix("Async");
278-
let return_task = operation.return_task(false);
278+
let return_task = operation.invocation_return_task("Task");
279279

280280
let parameters = operation.non_streamed_parameters();
281281

@@ -393,7 +393,7 @@ if ({features_parameter}?.Get<IceRpc.Features.ICompressFeature>() is null)
393393

394394
fn proxy_base_operation_impl(operation: &Operation, namespace: &str) -> CodeBlock {
395395
let async_name = operation.escape_identifier_with_suffix("Async");
396-
let return_task = operation.return_task(false);
396+
let return_task = operation.invocation_return_task("Task");
397397
let mut operation_params = operation
398398
.parameters()
399399
.iter()
@@ -427,7 +427,7 @@ fn proxy_interface_operations(interface_def: &Interface) -> CodeBlock {
427427
for operation in operations {
428428
let mut builder = FunctionBuilder::new(
429429
"",
430-
&operation.return_task(false),
430+
&operation.invocation_return_task("Task"),
431431
&operation.escape_identifier_with_suffix("Async"),
432432
FunctionType::Declaration,
433433
);
@@ -517,8 +517,6 @@ fn request_class(interface_def: &Interface) -> CodeBlock {
517517
}
518518

519519
fn response_class(interface_def: &Interface) -> CodeBlock {
520-
let namespace = &interface_def.namespace();
521-
522520
let mut operations = interface_def.operations();
523521
operations.retain(|o| {
524522
// We need to generate a method to decode the responses of any operations with return members or any Slice1
@@ -541,30 +539,19 @@ fn response_class(interface_def: &Interface) -> CodeBlock {
541539
).add_generated_remark("static class", interface_def);
542540

543541
for operation in operations {
544-
let members = operation.return_members();
545-
546542
let function_type = if operation.streamed_return_member().is_some() || operation.encoding == Encoding::Slice1 {
547543
FunctionType::BlockBody
548544
} else {
549545
FunctionType::ExpressionBody
550546
};
551547

552-
let return_type = if members.is_empty() {
553-
"global::System.Threading.Tasks.ValueTask".to_owned()
554-
} else {
555-
format!(
556-
"global::System.Threading.Tasks.ValueTask<{}>",
557-
members.to_tuple_type(namespace, TypeContext::IncomingParam),
558-
)
559-
};
560-
561548
let mut builder = FunctionBuilder::new(
562549
if function_type == FunctionType::ExpressionBody {
563550
"public static"
564551
} else {
565552
"public static async"
566553
},
567-
&return_type,
554+
&operation.invocation_return_task("ValueTask"),
568555
&operation.escape_identifier_with_prefix_and_suffix("Decode", "Async"),
569556
function_type,
570557
);

tools/slicec-cs/src/slicec_ext/operation_ext.rs

Lines changed: 27 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
use super::{EntityExt, MemberExt, ParameterExt, ParameterSliceExt};
44
use crate::code_gen_util::TypeContext;
55
use crate::cs_attributes::CsEncodedReturn;
6-
use slicec::grammar::{AttributeFunctions, Contained, Operation};
6+
use slicec::grammar::{AttributeFunctions, Operation};
77

88
pub trait OperationExt {
99
/// Returns the format that classes should be encoded with.
1010
fn get_class_format(&self, is_dispatch: bool) -> &str;
1111

12-
/// The operation return task.
13-
fn return_task(&self, is_dispatch: bool) -> String;
12+
fn invocation_return_task(&self, task_type: &str) -> String;
13+
fn dispatch_return_task(&self) -> String;
1414
}
1515

1616
impl OperationExt for Operation {
@@ -25,50 +25,35 @@ impl OperationExt for Operation {
2525
}
2626
}
2727

28-
fn return_task(&self, is_dispatch: bool) -> String {
28+
fn invocation_return_task(&self, task_type: &str) -> String {
29+
let namespace = &self.namespace();
30+
let return_type = match self.return_members().as_slice() {
31+
[] => "".to_owned(),
32+
members => format!("<{}>", members.to_tuple_type(namespace, TypeContext::IncomingParam)),
33+
};
34+
format!("global::System.Threading.Tasks.{task_type}{return_type}")
35+
}
36+
37+
fn dispatch_return_task(&self) -> String {
2938
let return_members = self.return_members();
3039
if return_members.is_empty() {
31-
if is_dispatch {
32-
"global::System.Threading.Tasks.ValueTask".to_owned()
33-
} else {
34-
"global::System.Threading.Tasks.Task".to_owned()
35-
}
40+
"global::System.Threading.Tasks.ValueTask".to_owned()
3641
} else {
37-
let return_type = operation_return_type(
38-
self,
39-
is_dispatch,
40-
if is_dispatch {
41-
TypeContext::OutgoingParam
42+
let namespace = self.namespace();
43+
let return_type = if self.has_attribute::<CsEncodedReturn>() {
44+
if let Some(stream_member) = self.streamed_return_member() {
45+
format!(
46+
"(global::System.IO.Pipelines.PipeReader Payload, {} {})",
47+
stream_member.cs_type_string(&namespace, TypeContext::OutgoingParam),
48+
stream_member.field_name(),
49+
)
4250
} else {
43-
TypeContext::IncomingParam
44-
},
45-
);
46-
if is_dispatch {
47-
format!("global::System.Threading.Tasks.ValueTask<{return_type}>")
51+
"global::System.IO.Pipelines.PipeReader".to_owned()
52+
}
4853
} else {
49-
format!("global::System.Threading.Tasks.Task<{return_type}>")
50-
}
51-
}
52-
}
53-
}
54-
55-
fn operation_return_type(operation: &Operation, is_dispatch: bool, context: TypeContext) -> String {
56-
let namespace = operation.parent().namespace();
57-
if is_dispatch && operation.has_attribute::<CsEncodedReturn>() {
58-
if let Some(stream_member) = operation.streamed_return_member() {
59-
format!(
60-
"(global::System.IO.Pipelines.PipeReader Payload, {} {})",
61-
stream_member.cs_type_string(&namespace, context),
62-
stream_member.field_name(),
63-
)
64-
} else {
65-
"global::System.IO.Pipelines.PipeReader".to_owned()
66-
}
67-
} else {
68-
match operation.return_members().as_slice() {
69-
[] => "void".to_owned(),
70-
[member] => member.cs_type_string(&namespace, context),
71-
members => members.to_tuple_type(&namespace, context),
54+
return_members.to_tuple_type(&namespace, TypeContext::OutgoingParam)
55+
};
56+
format!("global::System.Threading.Tasks.ValueTask<{return_type}>")
7257
}
7358
}
7459
}

0 commit comments

Comments
 (0)