Skip to content

Commit ab12c39

Browse files
committed
Add Status::set_lossy and new_set_lossy
1 parent 9522b6f commit ab12c39

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/lib.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,13 @@ impl Status {
417417
Ok(status)
418418
}
419419

420+
/// Creates a status and sets its code and message.
421+
pub fn new_set_lossy(code: Code, msg: &str) -> Status {
422+
let mut status = Status::new();
423+
status.set_lossy(code, msg);
424+
status
425+
}
426+
420427
/// Returns the status's code.
421428
pub fn code(&self) -> Code {
422429
unsafe { Code::from_int(tf::TF_GetCode(self.inner) as u32) }
@@ -445,6 +452,26 @@ impl Status {
445452
Ok(())
446453
}
447454

455+
/// Sets the code and message.
456+
pub fn set_lossy(&mut self, code: Code, msg: &str) {
457+
let message = match CString::new(msg) {
458+
Ok(x) => x,
459+
Err(e) => {
460+
let pos = e.nul_position();
461+
let mut truncated_bytes = e.into_vec();
462+
truncated_bytes.truncate(pos);
463+
let mut new_msg: Vec<u8> = "(original error truncated due to internal nul byte) "
464+
.as_bytes()
465+
.into();
466+
new_msg.extend(&truncated_bytes);
467+
unsafe { CString::from_vec_unchecked(new_msg) }
468+
}
469+
};
470+
unsafe {
471+
tf::TF_SetStatus(self.inner, code.to_c(), message.as_ptr());
472+
}
473+
}
474+
448475
/// Returns a mutable pointer to the inner tensorflow Status `TF_Status`.
449476
fn inner(&mut self) -> *mut tf::TF_Status {
450477
self.inner

0 commit comments

Comments
 (0)