triton_rs/
lib.rs

1mod backend;
2mod model;
3mod request;
4
5pub use backend::Backend;
6pub use model::Model;
7pub use request::Request;
8pub use triton_sys as sys;
9
10pub type Error = Box<dyn std::error::Error>;
11
12pub(crate) fn check_err(err: *mut triton_sys::TRITONSERVER_Error) -> Result<(), Error> {
13    if !err.is_null() {
14        let code = unsafe { triton_sys::TRITONSERVER_ErrorCode(err) };
15        Err(format!(
16            "TRITONBACKEND_ModelInstanceModel returned error code {}",
17            code
18        )
19        .into())
20    } else {
21        Ok(())
22    }
23}
24
25pub fn decode_string(data: &[u8]) -> Result<Vec<String>, Error> {
26    let mut strings = vec![];
27    let mut i = 0;
28
29    while i < data.len() {
30        // l = struct.unpack_from("<I", val_buf, offset)[0]
31        // offset += 4
32        let wide = u32::from_le_bytes([data[i], data[i + 1], data[i + 2], data[i + 3]]) as usize;
33        i += 4;
34
35        // sb = struct.unpack_from("<{}s".format(l), val_buf, offset)[0]
36        // offset += l
37        // strs.append(sb)
38        let bytes = &data[i..i + wide];
39        let string = String::from_utf8_lossy(bytes).to_string();
40        i += wide;
41
42        strings.push(string);
43    }
44
45    Ok(strings)
46}
47
48pub fn encode_string(value: &str) -> Vec<u8> {
49    let mut bytes = vec![];
50
51    let value: Vec<u8> = value.bytes().collect();
52
53    // l = struct.unpack_from("<I", val_buf, offset)[0]
54    // offset += 4
55    let len = (value.len() as u32).to_le_bytes();
56    bytes.extend_from_slice(&len);
57
58    // sb = struct.unpack_from("<{}s".format(l), val_buf, offset)[0]
59    // offset += l
60    // strs.append(sb)
61    bytes.extend_from_slice(&value);
62
63    bytes
64}