Skip to content

Commit

Permalink
Merge Async and Gen into CoroutineKind
Browse files Browse the repository at this point in the history
  • Loading branch information
eholk committed Dec 4, 2023
1 parent 1ab7fc9 commit 97fdae1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 17 deletions.
24 changes: 16 additions & 8 deletions src/closures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub(crate) fn rewrite_closure(
binder: &ast::ClosureBinder,
constness: ast::Const,
capture: ast::CaptureBy,
is_async: &ast::Async,
coro_kind: &ast::CoroutineKind,
movability: ast::Movability,
fn_decl: &ast::FnDecl,
body: &ast::Expr,
Expand All @@ -40,7 +40,7 @@ pub(crate) fn rewrite_closure(
debug!("rewrite_closure {:?}", body);

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.
let body_shape = shape.offset_left(extra_offset)?;
Expand Down Expand Up @@ -233,7 +233,7 @@ fn rewrite_closure_fn_decl(
binder: &ast::ClosureBinder,
constness: ast::Const,
capture: ast::CaptureBy,
asyncness: &ast::Async,
coro_kind: &ast::CoroutineKind,
movability: ast::Movability,
fn_decl: &ast::FnDecl,
body: &ast::Expr,
Expand Down Expand Up @@ -263,7 +263,8 @@ fn rewrite_closure_fn_decl(
} 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 { .. }) {
"move "
} else {
Expand All @@ -272,7 +273,14 @@ fn rewrite_closure_fn_decl(
// 4 = "|| {".len(), which is overconservative when the closure consists of
// a single expression.
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)?;

// 1 = |
Expand Down Expand Up @@ -310,7 +318,7 @@ fn rewrite_closure_fn_decl(
.tactic(tactic)
.preserve_newline(true);
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 prefix.contains('\n') {
Expand Down Expand Up @@ -339,7 +347,7 @@ pub(crate) fn rewrite_last_closure(
ref binder,
constness,
capture_clause,
ref asyncness,
ref coro_kind,
movability,
ref fn_decl,
ref body,
Expand All @@ -360,7 +368,7 @@ pub(crate) fn rewrite_last_closure(
binder,
constness,
capture_clause,
asyncness,
coro_kind,
movability,
fn_decl,
body,
Expand Down
2 changes: 1 addition & 1 deletion src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ pub(crate) fn format_expr(
&cl.binder,
cl.constness,
cl.capture_clause,
&cl.asyncness,
&cl.coro_kind,
cl.movability,
&cl.fn_decl,
&cl.body,
Expand Down
8 changes: 4 additions & 4 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ pub(crate) struct FnSig<'a> {
decl: &'a ast::FnDecl,
generics: &'a ast::Generics,
ext: ast::Extern,
is_async: Cow<'a, ast::Async>,
coro_kind: Cow<'a, ast::CoroutineKind>,
constness: ast::Const,
defaultness: ast::Defaultness,
unsafety: ast::Unsafe,
Expand All @@ -302,7 +302,7 @@ impl<'a> FnSig<'a> {
) -> FnSig<'a> {
FnSig {
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,
defaultness: ast::Defaultness::Final,
ext: method_sig.header.ext,
Expand All @@ -328,7 +328,7 @@ impl<'a> FnSig<'a> {
generics,
ext: fn_sig.header.ext,
constness: fn_sig.header.constness,
is_async: Cow::Borrowed(&fn_sig.header.asyncness),
coro_kind: Cow::Borrowed(&fn_sig.header.coro_kind),
defaultness,
unsafety: fn_sig.header.unsafety,
visibility: vis,
Expand All @@ -343,7 +343,7 @@ impl<'a> FnSig<'a> {
result.push_str(&*format_visibility(context, self.visibility));
result.push_str(format_defaultness(self.defaultness));
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_extern(
self.ext,
Expand Down
9 changes: 5 additions & 4 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,11 @@ pub(crate) fn format_visibility(
}

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

Expand Down

0 comments on commit 97fdae1

Please sign in to comment.