microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
compiler/qsc_data_structures/src/functors.rs
28lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | use crate::display::join; |
| 5 | use std::{ |
| 6 | fmt::{self, Display, Formatter}, |
| 7 | iter, |
| 8 | }; |
| 9 | |
| 10 | /// A functor application. |
| 11 | #[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] |
| 12 | pub struct FunctorApp { |
| 13 | /// An invocation is either adjoint or not, with each successive use of `Adjoint` functor switching |
| 14 | /// between the two, so a bool is sufficient to track. |
| 15 | pub adjoint: bool, |
| 16 | |
| 17 | /// An invocation can have multiple `Controlled` functors with each one adding another layer of updates |
| 18 | /// to the argument tuple, so the functor application must be tracked with a count. |
| 19 | pub controlled: u8, |
| 20 | } |
| 21 | |
| 22 | impl Display for FunctorApp { |
| 23 | fn fmt(&self, f: &mut Formatter) -> fmt::Result { |
| 24 | let controlleds = iter::repeat("Controlled").take(self.controlled.into()); |
| 25 | let adjoint = iter::once("Adjoint").filter(|_| self.adjoint); |
| 26 | join(f, controlleds.chain(adjoint), " ") |
| 27 | } |
| 28 | } |
| 29 | |