Constant rerast_macros::_RERAST_MACROS_SRC [] [src]

pub const _RERAST_MACROS_SRC: &'static str = "// Copyright 2017 Google Inc.\n//\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n//     https://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\n\n// global `#[deny()]` in the processed crate affects this file as well,\n// and could cause compilation-breaking warnings.\n#![allow(missing_docs, warnings)]\n\n/// Invocations of this macro instruct Rerast to replace the first expression/statement with the one\n/// after the =>. For example replace!(5 => 6) says to replace all occurences of the literal 5 with\n/// the literal 6.\n///\n/// The macro should be invoked inside a function definition. The arguments to the function\n/// represent placeholders within the search and replace expressions.\n#[macro_export]\nmacro_rules! replace {\n    ($a:expr => $b:expr) => {\n        // Suppress warnings about unused values in case we\'re replacing something like a Result.\n        #[allow(unused_must_use)]\n        // If we have a search or replacement pattern that is just a placeholder then this macro\n        // will turn that into a statement. Normally a reference to a variable is not permitted as a\n        // statement, allow it.\n        #[allow(path_statements)]\n        #[allow(unreachable_code)]\n        {\n            if false {\n                use $crate::GetMode;\n                match $crate::_ExprRuleMarker.get_mode() {\n                    $crate::Mode::Search => {&$a;}\n                    $crate::Mode::Replace => {&$b;}\n                }\n                // Some of the placeholders used in this replace! invocation might have been\n                // moved. To avoid use-after-move errors, we mark this as unreachable.\n                unreachable!();\n            }\n        }\n    };\n}\n\n/// Replaces one type with another. This will not match trait bounds (e.g. in where clauses), since\n/// they are not types. If you want to replace all references to a trait, use `replace_trait_ref!`\n/// instead.\n#[macro_export]\nmacro_rules! replace_type {\n    ($a:ty => $b:ty) => {\n        {\n            use $crate::GetMode;\n            match $crate::_TypeRuleMarker.get_mode() {\n                $crate::Mode::Search => {\n                    let _t: &$a;\n                }\n                $crate::Mode::Replace => {\n                    let _t: &$b;\n                }\n            }\n        }\n    };\n}\n\n#[macro_export]\nmacro_rules! replace_trait_ref {\n    ($a:ty => $b:ty) => {\n        {\n            use $crate::GetMode;\n            match $crate::_TraitRefRuleMarker.get_mode() {\n                $crate::Mode::Search => {\n                    let _t: &$a;\n                }\n                $crate::Mode::Replace => {\n                    let _t: &$b;\n                }\n            }\n        }\n    };\n}\n\n/// Replaces a pattern with another pattern. A placeholder is required in order to specify the types\n/// of the search and replacement pattern. Although they can be the same if you\'re replacing a\n/// pattern with another of the same type.\n///\n/// # Examples\n///\n/// ```\n/// # #[macro_use] extern crate rerast;\n/// # struct Foo(i32);\n/// fn some_foo_to_result_foo(p1: Option<Foo>, p2: Result<Foo, ()>) {\n///     replace_pattern!(Some(f1) = p1 => Result::Ok(f1) = p2);\n///     replace_pattern!(None = p1 => Result::Err(()) = p2);\n/// }\n/// # fn main () {}\n/// ```\n///\n/// This will transform:\n///\n/// ```\n/// # #[derive(Copy, Clone)] struct Foo(i32);\n/// # fn f() -> Option<Foo> {Some(Foo(42))}\n/// match f() {\n///     Some(Foo(x)) => x,\n///     None => 0,\n/// }\n/// ```\n///\n/// Into:\n///\n/// ```\n/// # fn f() -> Foo {Foo(42)}\n/// match f() {\n///     Result::Ok(Foo(x)) => x,\n///     Result::Err(()) => 0,\n/// }\n/// ```\n#[macro_export]\nmacro_rules! replace_pattern {\n    ($a:pat = $at:ident => $b:pat = $bt:ident) => {\n        if false {\n            use $crate::GetMode;\n            match $crate::_PatternRuleMarker.get_mode() {\n                $crate::Mode::Search => {\n                    if let Some($a) = Some($at) {};\n                }\n                $crate::Mode::Replace => {\n                    if let Some($b) = Some($bt) {};\n                }\n            }\n            // Some of the placeholders used in this replace_pattern! invocation might have been\n            // moved. To avoid use-after-move errors, we mark this as unreachable.\n            unreachable!();\n        }\n    }\n}\n\n/// A placeholder that matches zero or more statements\n/// IMPORTANT: This is currently broken and will most likely be replaced with a macro.\npub type Statements = &\'static Fn() -> _Statements;\n\n// These types are internal markers to help rerast find and identify things in rules. They\'re not\n// intended to be referenced directly.\npub enum Mode {\n    Search,\n    Replace,\n}\npub struct _Statements;\npub trait GetMode {\n    fn get_mode(&self) -> Mode {\n        Mode::Search\n    }\n}\npub struct _TypeRuleMarker;\nimpl GetMode for _TypeRuleMarker {}\npub struct _ExprRuleMarker;\nimpl GetMode for _ExprRuleMarker {}\npub struct _PatternRuleMarker;\nimpl GetMode for _PatternRuleMarker {}\npub struct _TraitRefRuleMarker;\nimpl GetMode for _TraitRefRuleMarker {}\n\npub const _RERAST_MACROS_SRC: &\'static str = include_str!(\"lib.rs\");\n"