summaryrefslogtreecommitdiff
path: root/src/utils.rs
diff options
context:
space:
mode:
authorMaxVerevkin <maxxverrr@gmail.com>2023-09-07 00:47:28 +0300
committerMaxVerevkin <maxxverrr@gmail.com>2023-09-07 00:47:28 +0300
commit74cc46a2383db5538d7e0392e4b79b7287775e9b (patch)
tree6f5c2c026c0822dd71a645836eb085677a090d4c /src/utils.rs
parenteefff9a9cc2ee6f17eab3431cc808d5a5e7d31fc (diff)
downloadi3bar-mango-74cc46a2383db5538d7e0392e4b79b7287775e9b.tar.gz
hyprland again
Diffstat (limited to 'src/utils.rs')
-rw-r--r--src/utils.rs30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/utils.rs b/src/utils.rs
index e45785f..211c6bb 100644
--- a/src/utils.rs
+++ b/src/utils.rs
@@ -1,8 +1,38 @@
//! Some usefull functions
+use std::io;
+use std::os::fd::{AsFd, AsRawFd};
+
use serde::Deserialize;
use serde_json::{Deserializer, Error as JsonError};
+/// Read from a raw file descriptor to the vector.
+///
+/// Appends data at the end of the buffer. Resizes vector as needed.
+#[allow(clippy::needless_pass_by_ref_mut)]
+pub fn read_to_vec(fd: &mut impl AsFd, buf: &mut Vec<u8>) -> io::Result<usize> {
+ if buf.capacity() - buf.len() < 1024 {
+ buf.reserve(buf.capacity().max(1024));
+ }
+
+ let res = unsafe {
+ libc::read(
+ fd.as_fd().as_raw_fd(),
+ buf.as_mut_ptr().add(buf.len()) as *mut libc::c_void,
+ (buf.capacity() - buf.len()) as libc::size_t,
+ )
+ };
+
+ if res == -1 {
+ return Err(io::Error::last_os_error());
+ }
+
+ let read = res as usize;
+ unsafe { buf.set_len(buf.len() + read) };
+
+ Ok(read)
+}
+
/// Retuns (`last_line`, `remaining`). See tests for examples.
pub fn last_line(s: &[u8]) -> Option<(&[u8], &[u8])> {
let mut it = memchr::memrchr_iter(b'\n', s);