mirror of
https://github.com/neondatabase/neon.git
synced 2026-01-12 07:52:55 +00:00
As the title says, I updated the lint rules to no longer allow unwrap or unimplemented. Three special cases: * Tests are allowed to use them * std::sync::Mutex lock().unwrap() is common because it's usually correct to continue panicking on poison * `tokio::spawn_blocking(...).await.unwrap()` is common because it will only error if the blocking fn panics, so continuing the panic is also correct I've introduced two extension traits to help with these last two, that are a bit more explicit so they don't need an expect message every time.
29 lines
734 B
Rust
29 lines
734 B
Rust
//! Small parsing helpers.
|
|
|
|
use std::ffi::CStr;
|
|
|
|
pub(crate) fn split_cstr(bytes: &[u8]) -> Option<(&CStr, &[u8])> {
|
|
let cstr = CStr::from_bytes_until_nul(bytes).ok()?;
|
|
let (_, other) = bytes.split_at(cstr.to_bytes_with_nul().len());
|
|
Some((cstr, other))
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn test_split_cstr() {
|
|
assert!(split_cstr(b"").is_none());
|
|
assert!(split_cstr(b"foo").is_none());
|
|
|
|
let (cstr, rest) = split_cstr(b"\0").expect("uh-oh");
|
|
assert_eq!(cstr.to_bytes(), b"");
|
|
assert_eq!(rest, b"");
|
|
|
|
let (cstr, rest) = split_cstr(b"foo\0bar").expect("uh-oh");
|
|
assert_eq!(cstr.to_bytes(), b"foo");
|
|
assert_eq!(rest, b"bar");
|
|
}
|
|
}
|