Skip to content

Commit 9b16ad9

Browse files
committed
add_filter using scoped
1 parent 7d2c1f4 commit 9b16ad9

File tree

1 file changed

+25
-24
lines changed

1 file changed

+25
-24
lines changed

src/header.rs

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -170,30 +170,31 @@ pub(crate) fn register_header(lua: &Lua) -> mlua::Result<()> {
170170
reg.add_function_mut(
171171
"add_filter",
172172
|_lua, (ud, tbl): (AnyUserData, HashMap<String, String>)| {
173-
let this = ud.borrow_mut::<HeaderView>()?;
174-
let c_str = std::ffi::CString::new(format!(
175-
r#"##FILTER=<ID={},Description="{}">"#,
176-
handle_hash_get(&tbl, "ID", "filter")?,
177-
handle_hash_get(&tbl, "Description", "filter")?,
178-
))
179-
.expect("CString::new failed");
180-
let ret =
181-
unsafe { rust_htslib::htslib::bcf_hdr_append(this.inner, c_str.as_ptr()) };
182-
if ret != 0 {
183-
log::error!("Error adding FILTER field for {:?}: {}", tbl, ret);
184-
return Err(mlua::Error::ExternalError(Arc::new(
185-
std::io::Error::last_os_error(),
186-
)));
187-
}
188-
let ret = unsafe { rust_htslib::htslib::bcf_hdr_sync(this.inner) };
189-
if ret != 0 {
190-
log::warn!(
191-
"Error syncing header after adding FILTER field for {:?}: {}",
192-
tbl,
193-
ret
194-
);
195-
}
196-
Ok(())
173+
ud.borrow_mut_scoped::<HeaderView, Result<(), mlua::Error>>(|this| {
174+
let c_str = std::ffi::CString::new(format!(
175+
r#"##FILTER=<ID={},Description="{}">"#,
176+
handle_hash_get(&tbl, "ID", "filter")?,
177+
handle_hash_get(&tbl, "Description", "filter")?,
178+
))
179+
.expect("CString::new failed");
180+
let ret =
181+
unsafe { rust_htslib::htslib::bcf_hdr_append(this.inner, c_str.as_ptr()) };
182+
if ret != 0 {
183+
log::error!("Error adding FILTER field for {:?}: {}", tbl, ret);
184+
return Err(mlua::Error::ExternalError(Arc::new(
185+
std::io::Error::last_os_error(),
186+
)));
187+
}
188+
let ret = unsafe { rust_htslib::htslib::bcf_hdr_sync(this.inner) };
189+
if ret != 0 {
190+
log::warn!(
191+
"Error syncing header after adding FILTER field for {:?}: {}",
192+
tbl,
193+
ret
194+
);
195+
}
196+
Ok(())
197+
})
197198
},
198199
);
199200
reg.add_function_mut(

0 commit comments

Comments
 (0)