| 1 | //! 0-size types for common separators |
| 2 | //! |
| 3 | //! This modules provides [`Display`](https://doc.rust-lang.org/std/fmt/trait.Display.html)-implementing |
| 4 | //! types for common separators. These types are 0-size, with fixed `Display` implementations, |
| 5 | //! intended to aid with compiler optimization. |
| 6 | |
| 7 | // NOTE: we hope that the compiler will detect that most operations on NoSeparator |
| 8 | // are no-ops, and optimize heavily, because I'd rather not implement a separate |
| 9 | // type for empty-separator-joins. |
| 10 | |
| 11 | use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::{selfrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/, Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display#, Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#}; |
| 12 | |
| 13 | use craterust-analyzer cargo joinery 3.1.0 crate/::joinrust-analyzer cargo joinery 3.1.0 join/::Separatorrust-analyzer cargo joinery 3.1.0 join/Separator#; |
| 14 | |
| 15 | /// Zero-size type representing the empty separator. |
| 16 | /// |
| 17 | /// This struct can be used as a separator in cases where you simply want to |
| 18 | /// join the elements of a separator without any elements between them. |
| 19 | /// |
| 20 | /// See also the [`join_concat`](crate::Joinable::join_concat) method. |
| 21 | /// |
| 22 | /// # Examples |
| 23 | /// |
| 24 | /// ``` |
| 25 | /// use joinery::JoinableIterator; |
| 26 | /// use joinery::separators::NoSeparator; |
| 27 | /// |
| 28 | /// let parts = (0..10); |
| 29 | /// let join = parts.join_with(NoSeparator); |
| 30 | /// assert_eq!(join.to_string(), "0123456789"); |
| 31 | /// ``` |
| 32 | /// |
| 33 | /// *New in 1.1.0* |
| 34 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:)] |
| 35 | #[must_use] |
| 36 | pub struct NoSeparatorrust-analyzer cargo joinery 3.1.0 separators/NoSeparator#; |
| 37 | |
| 38 | impl Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# for NoSeparatorrust-analyzer cargo joinery 3.1.0 separators/NoSeparator# { |
| 39 | #[inline(always)] |
| 40 | fn fmtrust-analyzer cargo joinery 3.1.0 separators/impl#[NoSeparator][Display]fmt().(&self, _f: &mut Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#) -> fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# { |
| 41 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | impl Separatorrust-analyzer cargo joinery 3.1.0 join/Separator# for NoSeparatorrust-analyzer cargo joinery 3.1.0 separators/NoSeparator# {} |
| 46 | |
| 47 | #[cfg(feature = "token-stream")] |
| 48 | impl quoterust-analyzer cargo quote 1.0.47 crate/::ToTokensrust-analyzer cargo quote 1.0.47 to_tokens/ToTokens# for NoSeparatorrust-analyzer cargo joinery 3.1.0 separators/NoSeparator# { |
| 49 | fn to_tokensrust-analyzer cargo joinery 3.1.0 separators/impl#[NoSeparator][ToTokens]to_tokens().(&self, _tokens: &mut proc_macro2rust-analyzer cargo proc-macro2 1.0.107 crate/::TokenStreamrust-analyzer cargo proc-macro2 1.0.107 TokenStream#) {} |
| 50 | } |
| 51 | |
| 52 | #[cfg(test)] |
| 53 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 54 | fn test_no_separatorrust-analyzer cargo joinery 3.1.0 separators/test_no_separator().() { |
| 55 | use craterust-analyzer cargo joinery 3.1.0 crate/::joinrust-analyzer cargo joinery 3.1.0 join/::Joinablerust-analyzer cargo joinery 3.1.0 join/Joinable#; |
| 56 | use craterust-analyzer cargo joinery 3.1.0 crate/::separatorsrust-analyzer cargo joinery 3.1.0 separators/::NoSeparatorrust-analyzer cargo joinery 3.1.0 separators/NoSeparator#; |
| 57 | |
| 58 | let data = [1, 2, 3, 4, 5]; |
| 59 | let join = data.join_withrust-analyzer cargo joinery 3.1.0 join/impl#[T][Joinable]join_with().(NoSeparatorrust-analyzer cargo joinery 3.1.0 separators/NoSeparator#); |
| 60 | let result = join.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(); |
| 61 | |
| 62 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(result, "12345"); |
| 63 | } |
| 64 | |
| 65 | macro_rules! const_separatorrust-analyzer cargo joinery 3.1.0 separators/const_separator! { |
| 66 | ($($Name:ident(sep: $sep:expr, repr: $repr:expr, test: $test_name:ident $(, token: $($token:tt)?)? ))+) => {$( |
| 67 | #[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)] |
| 68 | #[must_use] |
| 69 | #[doc = "Zero size type representing the "] |
| 70 | #[doc = $repr] |
| 71 | #[doc = " separator."] |
| 72 | pub struct $Name; |
| 73 | |
| 74 | impl Display for $Name { |
| 75 | #[inline] |
| 76 | fn fmt(&self, f: &mut Formatter) -> fmt::Result { |
| 77 | $sep.fmt(f) |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | impl Separator for $Name {} |
| 82 | |
| 83 | $( |
| 84 | #[cfg(feature="token-stream")] |
| 85 | impl quote::ToTokens for $Name { |
| 86 | fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) { |
| 87 | $( |
| 88 | tokens.extend(token!($token)); |
| 89 | )? |
| 90 | let _tokens = tokens; |
| 91 | } |
| 92 | } |
| 93 | )? |
| 94 | |
| 95 | #[cfg(test)] |
| 96 | mod $test_name { |
| 97 | use crate::separators::$Name; |
| 98 | use crate::join::Joinable; |
| 99 | |
| 100 | #[test] |
| 101 | fn separator() { |
| 102 | let data = [1, 2, 3]; |
| 103 | let join = data.join_with($Name); |
| 104 | let result = join.to_string(); |
| 105 | |
| 106 | assert_eq!(result, concat!(1, $sep, 2, $sep, 3)); |
| 107 | } |
| 108 | |
| 109 | $( |
| 110 | #[cfg(feature="token-stream")] |
| 111 | #[test] |
| 112 | fn to_tokens() { |
| 113 | use quote::{ToTokens, quote}; |
| 114 | |
| 115 | let data = [1, 2, 3]; |
| 116 | let join = data.join_with($Name); |
| 117 | let result = join.into_token_stream(); |
| 118 | |
| 119 | let target = quote! { |
| 120 | 1i32 $($token)? 2i32 $($token)? 3i32 |
| 121 | }; |
| 122 | |
| 123 | assert_eq!(result.to_string(), target.to_string()); |
| 124 | } |
| 125 | )? |
| 126 | } |
| 127 | )+} |
| 128 | } |
| 129 | |
| 130 | #[cfg(feature = "token-stream")] |
| 131 | macro_rules! tokenrust-analyzer cargo joinery 3.1.0 separators/token! { |
| 132 | (.) => { token!(token '.') }; |
| 133 | (,) => { token!(token ',') }; |
| 134 | (/) => { token!(token '/') }; |
| 135 | (-) => { token!(token '-') }; |
| 136 | |
| 137 | (token $token:literal) => { |
| 138 | [proc_macro2::TokenTree::Punct(proc_macro2::Punct::new( |
| 139 | $token, |
| 140 | proc_macro2::Spacing::Alone, |
| 141 | ))] |
| 142 | }; |
| 143 | } |
| 144 | |
| 145 | const_separatorrust-analyzer cargo joinery 3.1.0 separators/const_separator!! { |
| 146 | Newlinerust-analyzer cargo joinery 3.1.0 separators/Newline#(sep: '\n', repr: "newline", test: test_newlinerust-analyzer cargo joinery 3.1.0 separators/test_newline/, token: ) |
| 147 | Spacerust-analyzer cargo joinery 3.1.0 separators/Space#(sep: ' ', repr:"space", test: test_spacerust-analyzer cargo joinery 3.1.0 separators/test_space/, token: ) |
| 148 | Commarust-analyzer cargo joinery 3.1.0 separators/Comma#(sep: ',', repr: "`,`", test: test_commarust-analyzer cargo joinery 3.1.0 separators/test_comma/, token: ,) |
| 149 | CommaSpacerust-analyzer cargo joinery 3.1.0 separators/CommaSpace#(sep: ", ", repr: "comma followed by space", test: test_comma_spacerust-analyzer cargo joinery 3.1.0 separators/test_comma_space/, token: ,) |
| 150 | Dotrust-analyzer cargo joinery 3.1.0 separators/Dot#(sep: '.', repr: "`.`", test: test_dotrust-analyzer cargo joinery 3.1.0 separators/test_dot/, token: .) |
| 151 | Slashrust-analyzer cargo joinery 3.1.0 separators/Slash#(sep: '/', repr: "`/`", test: test_slashrust-analyzer cargo joinery 3.1.0 separators/test_slash/, token: /) |
| 152 | Underscorerust-analyzer cargo joinery 3.1.0 separators/Underscore#(sep: '_', repr: "`_`", test: test_underscorerust-analyzer cargo joinery 3.1.0 separators/test_underscore/) |
| 153 | Dashrust-analyzer cargo joinery 3.1.0 separators/Dash#(sep: '-', repr: "`-`", test: test_dashrust-analyzer cargo joinery 3.1.0 separators/test_dash/, token: -) |
| 154 | Tabrust-analyzer cargo joinery 3.1.0 separators/Tab#(sep: '\t', repr: "tab", test: test_tabrust-analyzer cargo joinery 3.1.0 separators/test_tab/, token: ) |
| 155 | } |