on master help sign in
remove unsafe
commitfa4caf7e3d43a40073f6966d37f529d5197bd840 authorBrian Shu <littlebubu.shu@gmail.com> dateThu 26 Aug 2021, 14:45 (5 years ago) parentcec5d43
2 files changed+6−13
helix-core/src/transaction.rs+1−1 helix-view/src/document.rs+5−12
helix-core/src/transaction.rs+1−1
@@ -125,7 +125,7 @@ impl ChangeSet {
125125 /// In other words, If `this` goes `docA` → `docB` and `other` represents `docB` → `docC`, the
126126 /// returned value will represent the change `docA` → `docC`.
127127 pub fn compose(self, other: Self) -> Self {
128− debug_assert!(self.len_after == other.len);
128+ assert!(self.len_after == other.len);
129129
130130 // composing fails in weird ways if one of the sets is empty
131131 // a: [] len: 0 len_after: 1 | b: [Insert(Tendril<UTF8>(inline: "\n")), Retain(1)] len 1
helix-view/src/document.rs+5−12
@@ -102,7 +102,7 @@ pub struct Document {
102102 language_server: Option<Arc<helix_lsp::Client>>,
103103}
104104
105−use std::fmt;
105+use std::{fmt, mem};
106106impl fmt::Debug for Document {
107107 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
108108 f.debug_struct("Document")
@@ -301,20 +301,13 @@ pub async fn to_writer<'a, W: tokio::io::AsyncWriteExt + Unpin + ?Sized>(
301301 Ok(())
302302}
303303
304−/// Like std::mem::replace() except it allows the replacement value to be mapped from the
305−/// original value.
306−fn take_with<T, F>(mut_ref: &mut T, closure: F)
304+fn take_with<T, F>(mut_ref: &mut T, f: F)
307305where
306+ T: Default,
308307 F: FnOnce(T) -> T,
309308{
310− use std::{panic, ptr};
311−
312− unsafe {
313− let old_t = ptr::read(mut_ref);
314− let new_t = panic::catch_unwind(panic::AssertUnwindSafe(|| closure(old_t)))
315− .unwrap_or_else(|_| ::std::process::abort());
316− ptr::write(mut_ref, new_t);
317− }
309+ let t = mem::take(mut_ref);
310+ let _ = mem::replace(mut_ref, f(t));
318311}
319312
320313use helix_lsp::lsp;