Skip to content

Commit cc12ae0

Browse files
committed
fix: make cabi compatible with older rustc
1 parent 0cbc8c5 commit cc12ae0

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

minijinja-cabi/src/error.rs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,21 @@ thread_local! {
1313
/// Returns `true` if there is currently an error.
1414
#[no_mangle]
1515
pub extern "C" fn mj_err_is_set() -> bool {
16-
LAST_ERROR.with_borrow(|x| x.is_some())
16+
LAST_ERROR.with(|x| x.borrow().is_some())
1717
}
1818

1919
/// Clears the current error.
2020
#[no_mangle]
2121
pub extern "C" fn mj_err_clear() {
22-
LAST_ERROR.with_borrow_mut(|x| *x = None);
22+
LAST_ERROR.with(|x| *x.borrow_mut() = None);
2323
}
2424

2525
/// Prints the error to stderr.
2626
#[no_mangle]
2727
pub extern "C" fn mj_err_print() -> bool {
28-
LAST_ERROR.with_borrow(|x| {
29-
if let Some(err) = x {
28+
LAST_ERROR.with(|x| {
29+
let x = x.borrow();
30+
if let Some(err) = x.as_ref() {
3031
eprintln!("error: {err}");
3132
if err.name().is_some() {
3233
eprintln!("{}", err.display_debug_info());
@@ -55,8 +56,9 @@ pub extern "C" fn mj_err_print() -> bool {
5556
#[no_mangle]
5657
pub unsafe extern "C" fn mj_err_get_debug_info() -> *mut c_char {
5758
LAST_ERROR
58-
.with_borrow(|x| {
59-
x.as_ref()
59+
.with(|x| {
60+
x.borrow()
61+
.as_ref()
6062
.and_then(|x| {
6163
let mut info = String::new();
6264
if x.name().is_some() {
@@ -92,8 +94,9 @@ pub unsafe extern "C" fn mj_err_get_debug_info() -> *mut c_char {
9294
#[no_mangle]
9395
pub unsafe extern "C" fn mj_err_get_detail() -> *mut c_char {
9496
LAST_ERROR
95-
.with_borrow(|x| {
96-
x.as_ref()
97+
.with(|x| {
98+
x.borrow()
99+
.as_ref()
97100
.and_then(|x| x.detail())
98101
.and_then(|detail| CString::new(detail).ok())
99102
.map(|cstr| cstr.into_raw())
@@ -107,8 +110,9 @@ pub unsafe extern "C" fn mj_err_get_detail() -> *mut c_char {
107110
#[no_mangle]
108111
pub unsafe extern "C" fn mj_err_get_template_name() -> *mut c_char {
109112
LAST_ERROR
110-
.with_borrow(|x| {
111-
x.as_ref()
113+
.with(|x| {
114+
x.borrow()
115+
.as_ref()
112116
.and_then(|x| x.name())
113117
.and_then(|name| CString::new(name).ok())
114118
.map(|cstr| cstr.into_raw())
@@ -120,7 +124,7 @@ pub unsafe extern "C" fn mj_err_get_template_name() -> *mut c_char {
120124
#[no_mangle]
121125
pub unsafe extern "C" fn mj_err_get_line() -> u32 {
122126
LAST_ERROR
123-
.with_borrow(|x| x.as_ref().and_then(|x| x.line()))
127+
.with(|x| x.borrow().as_ref().and_then(|x| x.line()))
124128
.unwrap_or(0) as _
125129
}
126130

@@ -180,8 +184,9 @@ impl TryFrom<ErrorKind> for mj_err_kind {
180184
#[no_mangle]
181185
pub unsafe extern "C" fn mj_err_get_kind() -> mj_err_kind {
182186
LAST_ERROR
183-
.with_borrow(|x| {
184-
x.as_ref()
187+
.with(|x| {
188+
x.borrow()
189+
.as_ref()
185190
.and_then(|x| mj_err_kind::try_from(x.kind()).ok())
186191
})
187192
.unwrap_or(mj_err_kind::MJ_ERR_KIND_UNKNOWN)

minijinja-cabi/src/utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ impl Scope {
6363
}
6464

6565
pub(crate) fn catch<F: FnOnce(&Scope) -> Result<R, Error>, R: AbiResult>(f: F) -> R {
66-
LAST_ERROR.with_borrow_mut(|x| *x = None);
66+
LAST_ERROR.with(|x| *x.borrow_mut() = None);
6767
match f(&Scope) {
6868
Ok(result) => result,
6969
Err(err) => {
70-
LAST_ERROR.with_borrow_mut(|x| *x = Some(err));
70+
LAST_ERROR.with(|x| *x.borrow_mut() = Some(err));
7171
R::err_value()
7272
}
7373
}

0 commit comments

Comments
 (0)