Преглед изворни кода

use line!, col!, and row! instead of panic location

Evan Almloff пре 3 година
родитељ
комит
dd335cda59
2 измењених фајлова са 18 додато и 10 уклоњено
  1. 1 1
      packages/core-macro/src/lib.rs
  2. 17 9
      packages/rsx_interpreter/src/lib.rs

+ 1 - 1
packages/core-macro/src/lib.rs

@@ -47,7 +47,7 @@ pub fn rsx(s: TokenStream) -> TokenStream {
                     Ok(captured) => {
                         let lazy = quote::quote! {
                             LazyNodes::new(move |__cx|{
-                                let code_location = get_line_num();
+                                let code_location = get_line_num!();
                                 let captured = #captured;
                                 let text = #rsx_text;
 

+ 17 - 9
packages/rsx_interpreter/src/lib.rs

@@ -6,7 +6,6 @@ use interperter::build;
 use lazy_static::lazy_static;
 use serde::{Deserialize, Serialize};
 use std::collections::HashMap;
-use std::panic::Location;
 use std::sync::{RwLock, RwLockReadGuard};
 use syn::parse_str;
 
@@ -79,14 +78,23 @@ fn interpert_rsx<'a, 'b>(
 }
 
 /// get the code location of the code that called this function
-#[track_caller]
-pub fn get_line_num() -> CodeLocation {
-    let location = Location::caller();
-    CodeLocation {
-        file: location.file().to_string(),
-        line: location.line(),
-        column: location.column(),
-    }
+#[macro_export]
+macro_rules! get_line_num {
+    () => {{
+        let line = line!();
+        let column = column!();
+        let file = file!();
+
+        #[cfg(windows)]
+        let file = env!("CARGO_MANIFEST_DIR").to_string() + "\\" + file!();
+        #[cfg(unix)]
+        let file = env!("CARGO_MANIFEST_DIR").to_string() + "/" + file!();
+        CodeLocation {
+            file: file.to_string(),
+            line: line,
+            column: column,
+        }
+    }};
 }
 
 /// A handle to the rsx context with interior mutability