Quellcodebibliothek Statistik Leitseite products/Sources/formale Sprachen/C/Android/hardware/hardware/nxp/nxp_mcu_loader/src/   (Android Betriebssystem Version 17©)  Datei vom 26.5.2026 mit Größe 2 kB image not shown  

Quelle  file_util.rs

  Sprache: Rust
 

use std::io;

#[allow(unused)]
pub trait Nonblocking {
    fn get_nonblocking(&self) -> io::Result<bool>;
    fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()>;
}

impl Nonblocking for std::fs::File {
    fn get_nonblocking(&self) -> io::Result<bool> {
        use nix::fcntl::{fcntl, FcntlArg, OFlag};
        use std::os::unix::io::AsRawFd;

        let fd = self.as_raw_fd();
        let raw_flags = fcntl(fd, FcntlArg::F_GETFL)?;
        let flags = OFlag::from_bits_truncate(raw_flags);

        Ok(flags.contains(OFlag::O_NONBLOCK))
    }

    fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
        use nix::fcntl::{fcntl, FcntlArg, OFlag};
        use std::os::unix::io::AsRawFd;

        let fd = self.as_raw_fd();
        let raw_flags = fcntl(fd, FcntlArg::F_GETFL)?;
        let mut flags = OFlag::from_bits_truncate(raw_flags);

        if nonblocking {
            flags.insert(OFlag::O_NONBLOCK);
        } else {
            flags.remove(OFlag::O_NONBLOCK);
        }

        fcntl(fd, FcntlArg::F_SETFL(flags))?;
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use anyhow::Result;
    use nix::unistd::pipe;
    use std::io::Read;

    #[test]
    fn get_nonblocking_works() -> Result<()> {
        let (rfd, _wfd) = pipe().unwrap();
        let rfile = std::fs::File::from(rfd);
        assert!(!rfile.get_nonblocking()?);
        Ok(())
    }

    #[test]
    fn set_nonblocking_works() -> Result<()> {
        let (rfd, _wfd) = pipe().unwrap();
        let rfile = std::fs::File::from(rfd);
        assert!(!rfile.get_nonblocking()?);

        rfile.set_nonblocking(true)?;

        assert!(rfile.get_nonblocking()?);
        Ok(())
    }

    #[test]
    fn set_nonblocking_has_effect() -> Result<()> {
        let (rfd, _wfd) = pipe().unwrap();
        let mut rfile = std::fs::File::from(rfd);

        rfile.set_nonblocking(true)?;

        let mut buf = [0u8; 80];
        let err = rfile.read(&mut buf).unwrap_err();

        assert_eq!(err.kind(), std::io::ErrorKind::WouldBlock);

        Ok(())
    }
}

Messung V0.5 in Prozent
C=95 H=94 G=94

¤ Dauer der Verarbeitung: 0.11 Sekunden  (vorverarbeitet am  2026-06-28) ¤

*© Formatika GbR, Deutschland






Wurzel

Suchen

PVS Prover

Isabelle Prover

NIST Cobol Testsuite

Cephes Mathematical Library

Vienna Development Method

Haftungshinweis

Die Informationen auf dieser Webseite wurden nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit, noch Qualität der bereit gestellten Informationen zugesichert.

Bemerkung:

Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.