pub fn to_tokens_with<F: ?Sized>(&self, mut add_optional: &mut F, tokens: &mut TokenStream)
    where
        F: FnMut() -> bool,
    {
        use Syntax::*;

        let sp = self.span;

        let choice_arms_to_tokens =
            |add_optional: &mut dyn FnMut() -> bool, arms: &[Spanned<Syntax>]| -> TokenStream {
                let mut acc = TokenStream::new();
                for (i, choice) in arms.iter().enumerate() {
                    quote_spanned!(sp=> #i => ).to_tokens(&mut acc);
                    choice.to_tokens_with(add_optional, &mut acc);
                    if i < arms.len() - 1 || add_optional() {
                        quote_spanned!(sp=> ,).to_tokens(&mut acc);
                    }
                }
                acc
            };

        match &self.inner {
            Recv(t) => quote_spanned! {sp=> recv #t },
            Send(t) => quote_spanned! {sp=> send #t },
            Call(callee) => {
                let mut acc = TokenStream::new();
                quote_spanned!(sp=> call ).to_tokens(&mut acc);

                if matches!(callee.inner, Block(_) | Type(_)) {
                    callee.to_tokens_with(add_optional, &mut acc);
                } else {
                    let callee_tokens = callee.to_token_stream_with(add_optional);
                    quote_spanned!(sp=> { #callee_tokens }).to_tokens(&mut acc);
                }

                acc
            }
            Split { tx_only, rx_only } => {
                let tx = tx_only.to_token_stream_with(add_optional);
                let rx = rx_only.to_token_stream_with(add_optional);
                quote_spanned! {sp=> split { -> #tx, <- #rx, } }
            }
            Choose(choices) => {
                let arms = choice_arms_to_tokens(&mut add_optional, choices);
                quote_spanned! {sp=> choose { #arms } }
            }
            Offer(choices) => {
                let arms = choice_arms_to_tokens(&mut add_optional, choices);
                quote_spanned! {sp=> offer { #arms } }
            }
            Loop(None, body) => {
                let body_tokens = body.to_token_stream_with(add_optional);
                quote_spanned! {sp=> loop #body_tokens }
            }
            Loop(Some(label), body) => {
                let lt = Lifetime::new(&format!("'{}", label), sp);
                let body_tokens = body.to_token_stream_with(add_optional);
                quote_spanned! {sp=> #lt: loop #body_tokens }
            }
            Break(None) => quote_spanned! {sp=> break },
            Break(Some(label)) => {
                let lt = Lifetime::new(&format!("'{}", label), sp);
                quote_spanned! {sp=> break #lt }
            }
            Continue(None) => quote_spanned! {sp=> continue },
            Continue(Some(label)) => {
                let lt = Lifetime::new(&format!("'{}", label), sp);
                quote_spanned! {sp=> continue #lt }
            }
            Block(stmts) => {
                let mut acc = TokenStream::new();

                // We want to separate the last statement from the rest so that we can decide
                // whether or not to put an (optional) terminator on it.
                if let Some((last, up_to_penultimate)) = stmts.split_last() {
                    for stmt in up_to_penultimate {
                        stmt.to_tokens_with(add_optional, &mut acc);

                        // If the statement is a call w/ a block argument then a semicolon is
                        // optional, rather than required.
                        let is_call_of_block = matches!(
                            &stmt.inner,
                            Call(callee) if matches!(&callee.inner, Block(_)),
                        );

                        // If the statement is a block, split, offer, choose, or loop construct,
                        // then it ends with a block/brace, and therefore a semicolon is optional,
                        // rather than required.
                        let ends_with_block = matches!(
                            &stmt.inner,
                            Block(_) | Split { .. } | Offer(_) | Choose(_) | Loop(_, _),
                        );

                        if !(is_call_of_block || ends_with_block) || add_optional() {
                            quote_spanned!(sp=> ;).to_tokens(&mut acc);
                        }
                    }

                    last.to_tokens_with(add_optional, &mut acc);

                    if add_optional() {
                        quote_spanned!(sp=> ;).to_tokens(&mut acc);
                    }
                }

                quote_spanned!(sp=> { #acc })
            }
            Type(ty) => quote_spanned! {sp=> #ty },
        }
        .to_tokens(tokens);
    }
/// Convert a `Spanned<Syntax>` into tokens and append them to the provided token stream, with a
/// predicate to determine whether or not optional syntax should be added or not.
///
/// The added optional syntax predicate is an `FnMut` closure so that we can randomly choose to
/// add or not add optional trailing commas and similar during testing.
Convert a `Spanned` into tokens and append them to the provided token stream, with a
predicate to determine whether or not optional syntax should be added or not.

The added optional syntax predicate is an `FnMut` closure so that we can randomly choose to
add or not add optional trailing commas and similar during testing.
Convert a ` Spanned ` into tokens and append them to the provided token stream with a predicate to determine whether or not optional syntax should be added or not . The added optional syntax predicate is an ` FnMut ` closure so that we can randomly choose to add or not add optional trailing commas and similar during testing .
pub fn to_tokens_with<F: ?Sized>(&self, mut add_optional: &mut F, tokens: &mut TokenStream)
    where
        F: FnMut() -> bool,
    {
        use Syntax::*;
        let sp = self.span;
        let choice_arms_to_tokens =
            |add_optional: &mut dyn FnMut() -> bool, arms: &[Spanned<Syntax>]| -> TokenStream {
                let mut acc = TokenStream::new();
                for (i, choice) in arms.iter().enumerate() {
                    quote_spanned!(sp=> #i => ).to_tokens(&mut acc);
                    choice.to_tokens_with(add_optional, &mut acc);
                    if i < arms.len() - 1 || add_optional() {
                        quote_spanned!(sp=> ,).to_tokens(&mut acc);
                    }
                }
                acc
            };
        match &self.inner {
            Recv(t) => quote_spanned! {sp=> recv #t },
            Send(t) => quote_spanned! {sp=> send #t },
            Call(callee) => {
                let mut acc = TokenStream::new();
                quote_spanned!(sp=> call ).to_tokens(&mut acc);
                if matches!(callee.inner, Block(_) | Type(_)) {
                    callee.to_tokens_with(add_optional, &mut acc);
                } else {
                    let callee_tokens = callee.to_token_stream_with(add_optional);
                    quote_spanned!(sp=> { #callee_tokens }).to_tokens(&mut acc);
                }
                acc
            }
            Split { tx_only, rx_only } => {
                let tx = tx_only.to_token_stream_with(add_optional);
                let rx = rx_only.to_token_stream_with(add_optional);
                quote_spanned! {sp=> split { -> #tx, <- #rx, } }
            }
            Choose(choices) => {
                let arms = choice_arms_to_tokens(&mut add_optional, choices);
                quote_spanned! {sp=> choose { #arms } }
            }
            Offer(choices) => {
                let arms = choice_arms_to_tokens(&mut add_optional, choices);
                quote_spanned! {sp=> offer { #arms } }
            }
            Loop(None, body) => {
                let body_tokens = body.to_token_stream_with(add_optional);
                quote_spanned! {sp=> loop #body_tokens }
            }
            Loop(Some(label), body) => {
                let lt = Lifetime::new(&format!("'{}", label), sp);
                let body_tokens = body.to_token_stream_with(add_optional);
                quote_spanned! {sp=> #lt: loop #body_tokens }
            }
            Break(None) => quote_spanned! {sp=> break },
            Break(Some(label)) => {
                let lt = Lifetime::new(&format!("'{}", label), sp);
                quote_spanned! {sp=> break #lt }
            }
            Continue(None) => quote_spanned! {sp=> continue },
            Continue(Some(label)) => {
                let lt = Lifetime::new(&format!("'{}", label), sp);
                quote_spanned! {sp=> continue #lt }
            }
            Block(stmts) => {
                let mut acc = TokenStream::new();
                // We want to separate the last statement from the rest so that we can decide
                // whether or not to put an (optional) terminator on it.
                if let Some((last, up_to_penultimate)) = stmts.split_last() {
                    for stmt in up_to_penultimate {
                        stmt.to_tokens_with(add_optional, &mut acc);
                        // If the statement is a call w/ a block argument then a semicolon is
                        // optional, rather than required.
                        let is_call_of_block = matches!(
                            &stmt.inner,
                            Call(callee) if matches!(&callee.inner, Block(_)),
                        );
                        // If the statement is a block, split, offer, choose, or loop construct,
                        // then it ends with a block/brace, and therefore a semicolon is optional,
                        // rather than required.
                        let ends_with_block = matches!(
                            &stmt.inner,
                            Block(_) | Split { .. } | Offer(_) | Choose(_) | Loop(_, _),
                        );
                        if !(is_call_of_block || ends_with_block) || add_optional() {
                            quote_spanned!(sp=> ;).to_tokens(&mut acc);
                        }
                    }
                    last.to_tokens_with(add_optional, &mut acc);
                    if add_optional() {
                        quote_spanned!(sp=> ;).to_tokens(&mut acc);
                    }
                }
                quote_spanned!(sp=> { #acc })
            }
            Type(ty) => quote_spanned! {sp=> #ty },
        }
        .to_tokens(tokens);
    }
pub fn to_tokens_with < F : ? Sized > ( & self , mut add_optional : & mut F , tokens : & mut TokenStream ) where F : FnMut ( ) -> bool , { use Syntax :: * ; let sp = self . span ; let choice_arms_to_tokens = | add_optional : & mut dyn FnMut ( ) -> bool , arms : & [ Spanned < Syntax > ] | -> TokenStream { let mut acc = TokenStream :: new ( ) ; for ( i , choice ) in arms . iter ( ) . enumerate ( ) { quote_spanned ! ( sp i ) . to_tokens ( & mut acc ) ; choice . to_tokens_with ( add_optional , & mut acc ) ; if i < arms . len ( ) - 1 || add_optional ( ) { quote_spanned ! ( sp ) . to_tokens ( & mut acc ) ; } } acc } ; match & self . inner { Recv ( t ) => quote_spanned ! { sp recv t } , Send ( t ) => quote_spanned ! { sp send t } , Call ( callee ) => { let mut acc = TokenStream :: new ( ) ; quote_spanned ! ( sp call ) . to_tokens ( & mut acc ) ; if matches ! ( callee inner Block ( ) Type ( ) ) { callee . to_tokens_with ( add_optional , & mut acc ) ; } else { let callee_tokens = callee . to_token_stream_with ( add_optional ) ; quote_spanned ! ( sp { callee_tokens } ) . to_tokens ( & mut acc ) ; } acc } Split { tx_only , rx_only } => { let tx = tx_only . to_token_stream_with ( add_optional ) ; let rx = rx_only . to_token_stream_with ( add_optional ) ; quote_spanned ! { sp split { tx rx } } } Choose ( choices ) => { let arms = choice_arms_to_tokens ( & mut add_optional , choices ) ; quote_spanned ! { sp choose { arms } } } Offer ( choices ) => { let arms = choice_arms_to_tokens ( & mut add_optional , choices ) ; quote_spanned ! { sp offer { arms } } } Loop ( None , body ) => { let body_tokens = body . to_token_stream_with ( add_optional ) ; quote_spanned ! { sp loop body_tokens } } Loop ( Some ( label ) , body ) => { let lt = Lifetime :: new ( & format ! ( " " label ) , sp ) ; let body_tokens = body . to_token_stream_with ( add_optional ) ; quote_spanned ! { sp lt loop body_tokens } } Break ( None ) => quote_spanned ! { sp break } , Break ( Some ( label ) ) => { let lt = Lifetime :: new ( & format ! ( " " label ) , sp ) ; quote_spanned ! { sp break lt } } Continue ( None ) => quote_spanned ! { sp continue } , Continue ( Some ( label ) ) => { let lt = Lifetime :: new ( & format ! ( " " label ) , sp ) ; quote_spanned ! { sp continue lt } } Block ( stmts ) => { let mut acc = TokenStream :: new ( ) ; // We want to separate the last statement from the rest so that we can decide // whether or not to put an (optional) terminator on it. if let Some ( ( last , up_to_penultimate ) ) = stmts . split_last ( ) { for stmt in up_to_penultimate { stmt . to_tokens_with ( add_optional , & mut acc ) ; // If the statement is a call w/ a block argument then a semicolon is // optional, rather than required. let is_call_of_block = matches ! ( stmt inner Call ( callee ) if matches ( callee inner Block ( ) ) ) ; // If the statement is a block, split, offer, choose, or loop construct, // then it ends with a block/brace, and therefore a semicolon is optional, // rather than required. let ends_with_block = matches ! ( stmt inner Block ( ) Split { } Offer ( ) Choose ( ) Loop ( ) ) ; if ! ( is_call_of_block || ends_with_block ) || add_optional ( ) { quote_spanned ! ( sp ) . to_tokens ( & mut acc ) ; } } last . to_tokens_with ( add_optional , & mut acc ) ; if add_optional ( ) { quote_spanned ! ( sp ) . to_tokens ( & mut acc ) ; } } quote_spanned ! ( sp { acc } ) } Type ( ty ) => quote_spanned ! { sp ty } , } . to_tokens ( tokens ) ; }
Convert a `Spanned<Syntax>` into tokens and append them to the provided token stream, with a
predicate to determine whether or not optional syntax should be added or not.
Convert a ` Spanned<Syntax > ` into tokens and append them to the provided token stream with a predicate to determine whether or not optional syntax should be added or not .
&self
add_optional
&mut F
tokens
&mut TokenStream