Skip to content

Commit 10cd6cf

Browse files
committed
use scoped
1 parent ce70d48 commit 10cd6cf

File tree

1 file changed

+46
-47
lines changed

1 file changed

+46
-47
lines changed

src/header.rs

Lines changed: 46 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -96,25 +96,25 @@ pub(crate) fn register_header(lua: &Lua) -> mlua::Result<()> {
9696
},
9797
);
9898
reg.add_meta_function(MetaMethod::ToString, |_lua, this: AnyUserData| {
99-
let this = this.borrow::<WrapHeaderView>()?;
100-
101-
let mut kstr = rust_htslib::htslib::kstring_t {
102-
l: 0,
103-
m: 0,
104-
s: std::ptr::null_mut(),
105-
};
106-
if unsafe { rust_htslib::htslib::bcf_hdr_format(this.0.inner, 0, &mut kstr) } != 0 {
107-
return Err(mlua::Error::ExternalError(Arc::new(
108-
std::io::Error::last_os_error(),
109-
)));
110-
}
111-
let s = unsafe {
112-
String::from_utf8_unchecked(
113-
std::slice::from_raw_parts(kstr.s as *const u8, kstr.l as usize).to_vec(),
114-
)
115-
};
99+
this.borrow_scoped::<WrapHeaderView, Result<String, mlua::Error>>(|this| {
100+
let mut kstr = rust_htslib::htslib::kstring_t {
101+
l: 0,
102+
m: 0,
103+
s: std::ptr::null_mut(),
104+
};
105+
if unsafe { rust_htslib::htslib::bcf_hdr_format(this.0.inner, 0, &mut kstr) } != 0 {
106+
return Err(mlua::Error::ExternalError(Arc::new(
107+
std::io::Error::last_os_error(),
108+
)));
109+
}
110+
let s = unsafe {
111+
String::from_utf8_unchecked(
112+
std::slice::from_raw_parts(kstr.s as *const u8, kstr.l as usize).to_vec(),
113+
)
114+
};
116115

117-
Ok(s)
116+
Ok(s)
117+
})
118118
});
119119
reg.add_field_method_get("samples", |_lua, this: &WrapHeaderView| {
120120
let samples = this
@@ -143,34 +143,34 @@ pub(crate) fn register_header(lua: &Lua) -> mlua::Result<()> {
143143
reg.add_function_mut(
144144
"add_info",
145145
|_lua, (ud, tbl): (AnyUserData, HashMap<String, String>)| {
146-
eprintln!("in add_info");
147-
let this = ud.borrow_mut::<WrapHeaderView>()?;
148-
eprintln!("got this");
149-
let c_str = std::ffi::CString::new(format!(
150-
r#"##INFO=<ID={},Number={},Type={},Description={}>"#,
151-
handle_hash_get(&tbl, "ID", "info")?,
152-
handle_hash_get(&tbl, "Number", "info")?,
153-
handle_hash_get(&tbl, "Type", "info")?,
154-
handle_hash_get(&tbl, "Description", "info")?,
155-
))
156-
.expect("CString::new failed");
157-
let ret =
158-
unsafe { rust_htslib::htslib::bcf_hdr_append(this.0.inner, c_str.as_ptr()) };
159-
if ret != 0 {
160-
log::error!("Error adding INFO field for {:?}: {}", tbl, ret);
161-
return Err(mlua::Error::ExternalError(Arc::new(
162-
std::io::Error::last_os_error(),
163-
)));
164-
}
165-
let ret = unsafe { rust_htslib::htslib::bcf_hdr_sync(this.0.inner) };
166-
if ret != 0 {
167-
log::warn!(
168-
"Error syncing header after adding INFO field for {:?}: {}",
169-
tbl,
170-
ret
171-
);
172-
}
173-
Ok(())
146+
ud.borrow_mut_scoped::<WrapHeaderView, Result<(), mlua::Error>>(|this| {
147+
let c_str = std::ffi::CString::new(format!(
148+
r#"##INFO=<ID={},Number={},Type={},Description={}>"#,
149+
handle_hash_get(&tbl, "ID", "info")?,
150+
handle_hash_get(&tbl, "Number", "info")?,
151+
handle_hash_get(&tbl, "Type", "info")?,
152+
handle_hash_get(&tbl, "Description", "info")?,
153+
))
154+
.expect("CString::new failed");
155+
let ret = unsafe {
156+
rust_htslib::htslib::bcf_hdr_append(this.0.inner, c_str.as_ptr())
157+
};
158+
if ret != 0 {
159+
log::error!("Error adding INFO field for {:?}: {}", tbl, ret);
160+
return Err(mlua::Error::ExternalError(Arc::new(
161+
std::io::Error::last_os_error(),
162+
)));
163+
}
164+
let ret = unsafe { rust_htslib::htslib::bcf_hdr_sync(this.0.inner) };
165+
if ret != 0 {
166+
log::warn!(
167+
"Error syncing header after adding INFO field for {:?}: {}",
168+
tbl,
169+
ret
170+
);
171+
}
172+
Ok(())
173+
})
174174
},
175175
);
176176
reg.add_function_mut(
@@ -295,7 +295,6 @@ mod tests {
295295
.set_name("test_add_info")
296296
.into_function()
297297
.expect("error in test_add_info");
298-
299298
lua.scope(|scope| {
300299
globals.set(
301300
"header",

0 commit comments

Comments
 (0)