Add verify macros & use in one location for point conversion

This commit is contained in:
Julia 2022-11-28 23:34:38 -05:00
parent 3a1cd6ed3a
commit 5965113fc8
6 changed files with 61 additions and 5 deletions

9
Cargo.lock generated
View file

@ -4778,6 +4778,7 @@ dependencies = [
"smallvec",
"sum_tree",
"util",
"verify",
]
[[package]]
@ -6844,6 +6845,14 @@ version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
[[package]]
name = "verify"
version = "0.1.0"
dependencies = [
"backtrace",
"log",
]
[[package]]
name = "version_check"
version = "0.9.4"

View file

@ -52,6 +52,7 @@ members = [
"crates/theme_selector",
"crates/theme_testbench",
"crates/util",
"crates/verify",
"crates/vim",
"crates/workspace",
"crates/zed",

View file

@ -12,7 +12,7 @@ smallvec = { version = "1.6", features = ["union"] }
sum_tree = { path = "../sum_tree" }
arrayvec = "0.7.1"
log = { version = "0.4.16", features = ["kv_unstable_serde"] }
verify = { path = "../verify" }
[dev-dependencies]
rand = "0.8.3"

View file

@ -12,6 +12,7 @@ use std::{
str,
};
use sum_tree::{Bias, Dimension, SumTree};
use verify::{verify, verify_not};
pub use offset_utf16::OffsetUtf16;
pub use point::Point;
@ -680,10 +681,11 @@ impl Chunk {
let mut offset = 0;
let mut point = Point::new(0, 0);
for ch in self.0.chars() {
if point >= target {
if point > target {
panic!("point {:?} is inside of character {:?}", target, ch);
}
verify_not!(point > target, ("point {:?} is inside of character {:?}", target, ch), else {
point = target;
});
if point == target {
break;
}

11
crates/verify/Cargo.toml Normal file
View file

@ -0,0 +1,11 @@
[package]
name = "verify"
version = "0.1.0"
edition = "2021"
[lib]
path = "src/verify.rs"
[dependencies]
backtrace = "0.3"
log = "0.4"

View file

@ -0,0 +1,33 @@
pub use backtrace::Backtrace;
#[macro_export]
macro_rules! verify {
( $expression:expr, else $block:expr ) => {
verify!($expression, (""), else $block)
};
( $expression:expr, ( $($fmt_arg:tt)* ), else $block:expr ) => {{
let verify_str = stringify!($expression);
if !$expression {
if cfg!(debug_assertions) {
panic!("Claim failed {:?}: {}", verify_str, format_args!($($fmt_arg)*));
} else {
let backtrace = $crate::Backtrace::new();
log::error!("Claim failed {:?}\n{:?}", verify_str, backtrace);
$block
}
}
}};
}
#[macro_export]
macro_rules! verify_not {
( $expression:expr, else $block:expr ) => {
verify_not!($expression, (""), else $block)
};
( $expression:expr, ( $($fmt_arg:tt)* ), else $block:expr ) => {
verify!(!$expression, ( $($fmt_arg)* ), else $block)
};
}