]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/common/mod.rs
cacb128faa560438f5447a50112be3d3a96f0dea
[rust.git] / src / libstd / sys / common / mod.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 #![allow(missing_docs)]
12 #![allow(dead_code)]
13
14 use io::{mod, IoError, IoResult};
15 use prelude::*;
16 use sys::{last_error, retry, fs};
17 use c_str::CString;
18 use num::Int;
19 use path::BytesContainer;
20 use collections;
21
22 pub mod net;
23 pub mod helper_thread;
24
25 // common error constructors
26
27 pub fn eof() -> IoError {
28     IoError {
29         kind: io::EndOfFile,
30         desc: "end of file",
31         detail: None,
32     }
33 }
34
35 pub fn timeout(desc: &'static str) -> IoError {
36     IoError {
37         kind: io::TimedOut,
38         desc: desc,
39         detail: None,
40     }
41 }
42
43 pub fn short_write(n: uint, desc: &'static str) -> IoError {
44     IoError {
45         kind: if n == 0 { io::TimedOut } else { io::ShortWrite(n) },
46         desc: desc,
47         detail: None,
48     }
49 }
50
51 pub fn unimpl() -> IoError {
52     IoError {
53         kind: io::IoUnavailable,
54         desc: "operations not yet supported",
55         detail: None,
56     }
57 }
58
59 // unix has nonzero values as errors
60 pub fn mkerr_libc<T: Int>(ret: T) -> IoResult<()> {
61     if ret != Int::zero() {
62         Err(last_error())
63     } else {
64         Ok(())
65     }
66 }
67
68 pub fn keep_going(data: &[u8], f: |*const u8, uint| -> i64) -> i64 {
69     let origamt = data.len();
70     let mut data = data.as_ptr();
71     let mut amt = origamt;
72     while amt > 0 {
73         let ret = retry(|| f(data, amt));
74         if ret == 0 {
75             break
76         } else if ret != -1 {
77             amt -= ret as uint;
78             data = unsafe { data.offset(ret as int) };
79         } else {
80             return ret;
81         }
82     }
83     return (origamt - amt) as i64;
84 }
85
86 // traits for extracting representations from
87
88 pub trait AsFileDesc {
89     fn as_fd(&self) -> &fs::FileDesc;
90 }
91
92 pub trait ProcessConfig<K: BytesContainer, V: BytesContainer> {
93     fn program(&self) -> &CString;
94     fn args(&self) -> &[CString];
95     fn env(&self) -> Option<&collections::HashMap<K, V>>;
96     fn cwd(&self) -> Option<&CString>;
97     fn uid(&self) -> Option<uint>;
98     fn gid(&self) -> Option<uint>;
99     fn detach(&self) -> bool;
100 }