aboutsummaryrefslogtreecommitdiff
path: root/nix-rust/src/foreign.rs
blob: 7bce7753c05c9f3f3e63720bdfdb3eb13ff9c926 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/// A wrapper around Nix's Source class that provides the Read trait.
#[repr(C)]
pub struct Source {
    fun: extern "C" fn(this: *mut libc::c_void, data: &mut [u8]) -> usize,
    this: *mut libc::c_void,
}

impl std::io::Read for Source {
    fn read(&mut self, buf: &mut [u8]) -> std::result::Result<usize, std::io::Error> {
        let n = (self.fun)(self.this, buf);
        assert!(n <= buf.len());
        Ok(n)
    }
}