Merge Async and Gen into CoroutineKind

This commit is contained in:
Eric Holk 2023-11-30 14:54:39 -08:00
parent 1ab7fc99af
commit 97fdae1034
4 changed files with 26 additions and 17 deletions

View File

@ -29,7 +29,7 @@ pub(crate) fn rewrite_closure(
binder: &ast::ClosureBinder, binder: &ast::ClosureBinder,
constness: ast::Const, constness: ast::Const,
capture: ast::CaptureBy, capture: ast::CaptureBy,
is_async: &ast::Async, coro_kind: &ast::CoroutineKind,
movability: ast::Movability, movability: ast::Movability,
fn_decl: &ast::FnDecl, fn_decl: &ast::FnDecl,
body: &ast::Expr, body: &ast::Expr,
@ -40,7 +40,7 @@ pub(crate) fn rewrite_closure(
debug!("rewrite_closure {:?}", body); debug!("rewrite_closure {:?}", body);
let (prefix, extra_offset) = rewrite_closure_fn_decl( let (prefix, extra_offset) = rewrite_closure_fn_decl(
binder, constness, capture, is_async, movability, fn_decl, body, span, context, shape, binder, constness, capture, coro_kind, movability, fn_decl, body, span, context, shape,
)?; )?;
// 1 = space between `|...|` and body. // 1 = space between `|...|` and body.
let body_shape = shape.offset_left(extra_offset)?; let body_shape = shape.offset_left(extra_offset)?;
@ -233,7 +233,7 @@ fn rewrite_closure_fn_decl(
binder: &ast::ClosureBinder, binder: &ast::ClosureBinder,
constness: ast::Const, constness: ast::Const,
capture: ast::CaptureBy, capture: ast::CaptureBy,
asyncness: &ast::Async, coro_kind: &ast::CoroutineKind,
movability: ast::Movability, movability: ast::Movability,
fn_decl: &ast::FnDecl, fn_decl: &ast::FnDecl,
body: &ast::Expr, body: &ast::Expr,
@ -263,7 +263,8 @@ fn rewrite_closure_fn_decl(
} else { } else {
"" ""
}; };
let is_async = if asyncness.is_async() { "async " } else { "" }; let is_async = if coro_kind.is_async() { "async " } else { "" };
let is_gen = if coro_kind.is_gen() { "gen " } else { "" };
let mover = if matches!(capture, ast::CaptureBy::Value { .. }) { let mover = if matches!(capture, ast::CaptureBy::Value { .. }) {
"move " "move "
} else { } else {
@ -272,7 +273,14 @@ fn rewrite_closure_fn_decl(
// 4 = "|| {".len(), which is overconservative when the closure consists of // 4 = "|| {".len(), which is overconservative when the closure consists of
// a single expression. // a single expression.
let nested_shape = shape let nested_shape = shape
.shrink_left(binder.len() + const_.len() + immovable.len() + is_async.len() + mover.len())? .shrink_left(
binder.len()
+ const_.len()
+ immovable.len()
+ is_async.len()
+ is_gen.len()
+ mover.len(),
)?
.sub_width(4)?; .sub_width(4)?;
// 1 = | // 1 = |
@ -310,7 +318,7 @@ fn rewrite_closure_fn_decl(
.tactic(tactic) .tactic(tactic)
.preserve_newline(true); .preserve_newline(true);
let list_str = write_list(&item_vec, &fmt)?; let list_str = write_list(&item_vec, &fmt)?;
let mut prefix = format!("{binder}{const_}{immovable}{is_async}{mover}|{list_str}|"); let mut prefix = format!("{binder}{const_}{immovable}{is_async}{is_gen}{mover}|{list_str}|");
if !ret_str.is_empty() { if !ret_str.is_empty() {
if prefix.contains('\n') { if prefix.contains('\n') {
@ -339,7 +347,7 @@ pub(crate) fn rewrite_last_closure(
ref binder, ref binder,
constness, constness,
capture_clause, capture_clause,
ref asyncness, ref coro_kind,
movability, movability,
ref fn_decl, ref fn_decl,
ref body, ref body,
@ -360,7 +368,7 @@ pub(crate) fn rewrite_last_closure(
binder, binder,
constness, constness,
capture_clause, capture_clause,
asyncness, coro_kind,
movability, movability,
fn_decl, fn_decl,
body, body,

View File

@ -212,7 +212,7 @@ pub(crate) fn format_expr(
&cl.binder, &cl.binder,
cl.constness, cl.constness,
cl.capture_clause, cl.capture_clause,
&cl.asyncness, &cl.coro_kind,
cl.movability, cl.movability,
&cl.fn_decl, &cl.fn_decl,
&cl.body, &cl.body,

View File

@ -287,7 +287,7 @@ pub(crate) struct FnSig<'a> {
decl: &'a ast::FnDecl, decl: &'a ast::FnDecl,
generics: &'a ast::Generics, generics: &'a ast::Generics,
ext: ast::Extern, ext: ast::Extern,
is_async: Cow<'a, ast::Async>, coro_kind: Cow<'a, ast::CoroutineKind>,
constness: ast::Const, constness: ast::Const,
defaultness: ast::Defaultness, defaultness: ast::Defaultness,
unsafety: ast::Unsafe, unsafety: ast::Unsafe,
@ -302,7 +302,7 @@ pub(crate) fn from_method_sig(
) -> FnSig<'a> { ) -> FnSig<'a> {
FnSig { FnSig {
unsafety: method_sig.header.unsafety, unsafety: method_sig.header.unsafety,
is_async: Cow::Borrowed(&method_sig.header.asyncness), coro_kind: Cow::Borrowed(&method_sig.header.coro_kind),
constness: method_sig.header.constness, constness: method_sig.header.constness,
defaultness: ast::Defaultness::Final, defaultness: ast::Defaultness::Final,
ext: method_sig.header.ext, ext: method_sig.header.ext,
@ -328,7 +328,7 @@ pub(crate) fn from_fn_kind(
generics, generics,
ext: fn_sig.header.ext, ext: fn_sig.header.ext,
constness: fn_sig.header.constness, constness: fn_sig.header.constness,
is_async: Cow::Borrowed(&fn_sig.header.asyncness), coro_kind: Cow::Borrowed(&fn_sig.header.coro_kind),
defaultness, defaultness,
unsafety: fn_sig.header.unsafety, unsafety: fn_sig.header.unsafety,
visibility: vis, visibility: vis,
@ -343,7 +343,7 @@ fn to_str(&self, context: &RewriteContext<'_>) -> String {
result.push_str(&*format_visibility(context, self.visibility)); result.push_str(&*format_visibility(context, self.visibility));
result.push_str(format_defaultness(self.defaultness)); result.push_str(format_defaultness(self.defaultness));
result.push_str(format_constness(self.constness)); result.push_str(format_constness(self.constness));
result.push_str(format_async(&self.is_async)); result.push_str(format_coro(&self.coro_kind));
result.push_str(format_unsafety(self.unsafety)); result.push_str(format_unsafety(self.unsafety));
result.push_str(&format_extern( result.push_str(&format_extern(
self.ext, self.ext,

View File

@ -75,10 +75,11 @@ pub(crate) fn format_visibility(
} }
#[inline] #[inline]
pub(crate) fn format_async(is_async: &ast::Async) -> &'static str { pub(crate) fn format_coro(coro_kind: &ast::CoroutineKind) -> &'static str {
match is_async { match coro_kind {
ast::Async::Yes { .. } => "async ", ast::CoroutineKind::Async { .. } => "async ",
ast::Async::No => "", ast::CoroutineKind::Gen { .. } => "gen ",
ast::CoroutineKind::None => "",
} }
} }