]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/common/mod.rs
9a89b290980126967c22d0ed400c2d0d8e64f40a
[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
13 use old_io::{self, IoError, IoResult};
14 use prelude::v1::*;
15 use sys::{last_error, retry};
16 use ffi::CString;
17 use num::Int;
18
19 #[allow(deprecated)]
20 use old_path::BytesContainer;
21
22 use collections;
23
24 #[macro_use] pub mod helper_thread;
25
26 pub mod backtrace;
27 pub mod condvar;
28 pub mod mutex;
29 pub mod net;
30 pub mod net2;
31 pub mod rwlock;
32 pub mod stack;
33 pub mod thread;
34 pub mod thread_info;
35 pub mod thread_local;
36 pub mod wtf8;
37
38 // common error constructors
39
40 pub fn eof() -> IoError {
41     IoError {
42         kind: old_io::EndOfFile,
43         desc: "end of file",
44         detail: None,
45     }
46 }
47
48 pub fn timeout(desc: &'static str) -> IoError {
49     IoError {
50         kind: old_io::TimedOut,
51         desc: desc,
52         detail: None,
53     }
54 }
55
56 pub fn short_write(n: uint, desc: &'static str) -> IoError {
57     IoError {
58         kind: if n == 0 { old_io::TimedOut } else { old_io::ShortWrite(n) },
59         desc: desc,
60         detail: None,
61     }
62 }
63
64 pub fn unimpl() -> IoError {
65     IoError {
66         kind: old_io::IoUnavailable,
67         desc: "operations not yet supported",
68         detail: None,
69     }
70 }
71
72 // unix has nonzero values as errors
73 pub fn mkerr_libc<T: Int>(ret: T) -> IoResult<()> {
74     if ret != Int::zero() {
75         Err(last_error())
76     } else {
77         Ok(())
78     }
79 }
80
81 pub fn keep_going<F>(data: &[u8], mut f: F) -> i64 where
82     F: FnMut(*const u8, uint) -> i64,
83 {
84     let origamt = data.len();
85     let mut data = data.as_ptr();
86     let mut amt = origamt;
87     while amt > 0 {
88         let ret = retry(|| f(data, amt));
89         if ret == 0 {
90             break
91         } else if ret != -1 {
92             amt -= ret as uint;
93             data = unsafe { data.offset(ret as int) };
94         } else {
95             return ret;
96         }
97     }
98     return (origamt - amt) as i64;
99 }
100
101 /// A trait for viewing representations from std types
102 #[doc(hidden)]
103 pub trait AsInner<Inner: ?Sized> {
104     fn as_inner(&self) -> &Inner;
105 }
106
107 /// A trait for viewing representations from std types
108 #[doc(hidden)]
109 pub trait AsInnerMut<Inner: ?Sized> {
110     fn as_inner_mut(&mut self) -> &mut Inner;
111 }
112
113 /// A trait for extracting representations from std types
114 #[doc(hidden)]
115 pub trait IntoInner<Inner> {
116     fn into_inner(self) -> Inner;
117 }
118
119 /// A trait for creating std types from internal representations
120 #[doc(hidden)]
121 pub trait FromInner<Inner> {
122     fn from_inner(inner: Inner) -> Self;
123 }
124
125 #[doc(hidden)]
126 #[allow(deprecated)]
127 pub trait ProcessConfig<K: BytesContainer, V: BytesContainer> {
128     fn program(&self) -> &CString;
129     fn args(&self) -> &[CString];
130     fn env(&self) -> Option<&collections::HashMap<K, V>>;
131     fn cwd(&self) -> Option<&CString>;
132     fn uid(&self) -> Option<uint>;
133     fn gid(&self) -> Option<uint>;
134     fn detach(&self) -> bool;
135 }