]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/common/mod.rs
Merge pull request #20510 from tshepang/patch-6
[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::{self, IoError, IoResult};
15 use prelude::v1::*;
16 use sys::{last_error, retry};
17 use c_str::CString;
18 use num::Int;
19 use path::BytesContainer;
20 use collections;
21
22 pub mod backtrace;
23 pub mod condvar;
24 pub mod helper_thread;
25 pub mod mutex;
26 pub mod net;
27 pub mod rwlock;
28 pub mod stack;
29 pub mod thread;
30 pub mod thread_info;
31 pub mod thread_local;
32
33 // common error constructors
34
35 pub fn eof() -> IoError {
36     IoError {
37         kind: io::EndOfFile,
38         desc: "end of file",
39         detail: None,
40     }
41 }
42
43 pub fn timeout(desc: &'static str) -> IoError {
44     IoError {
45         kind: io::TimedOut,
46         desc: desc,
47         detail: None,
48     }
49 }
50
51 pub fn short_write(n: uint, desc: &'static str) -> IoError {
52     IoError {
53         kind: if n == 0 { io::TimedOut } else { io::ShortWrite(n) },
54         desc: desc,
55         detail: None,
56     }
57 }
58
59 pub fn unimpl() -> IoError {
60     IoError {
61         kind: io::IoUnavailable,
62         desc: "operations not yet supported",
63         detail: None,
64     }
65 }
66
67 // unix has nonzero values as errors
68 pub fn mkerr_libc<T: Int>(ret: T) -> IoResult<()> {
69     if ret != Int::zero() {
70         Err(last_error())
71     } else {
72         Ok(())
73     }
74 }
75
76 pub fn keep_going<F>(data: &[u8], mut f: F) -> i64 where
77     F: FnMut(*const u8, uint) -> i64,
78 {
79     let origamt = data.len();
80     let mut data = data.as_ptr();
81     let mut amt = origamt;
82     while amt > 0 {
83         let ret = retry(|| f(data, amt));
84         if ret == 0 {
85             break
86         } else if ret != -1 {
87             amt -= ret as uint;
88             data = unsafe { data.offset(ret as int) };
89         } else {
90             return ret;
91         }
92     }
93     return (origamt - amt) as i64;
94 }
95
96 // A trait for extracting representations from std::io types
97 pub trait AsInner<Inner> {
98     fn as_inner(&self) -> &Inner;
99 }
100
101 pub trait ProcessConfig<K: BytesContainer, V: BytesContainer> {
102     fn program(&self) -> &CString;
103     fn args(&self) -> &[CString];
104     fn env(&self) -> Option<&collections::HashMap<K, V>>;
105     fn cwd(&self) -> Option<&CString>;
106     fn uid(&self) -> Option<uint>;
107     fn gid(&self) -> Option<uint>;
108     fn detach(&self) -> bool;
109 }