| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 //! This module contains a variety of types which are used | |
| 6 //! for representing representing flag arguments a little bit | |
| 7 //! better than just as u32 and some other basic Mojo types that | |
| 8 //! we need to expose. | |
| 9 //! | |
| 10 //! This module also provides MojoResult which is the canonical | |
| 11 //! result coding system used by Mojo. | |
| 12 //! | |
| 13 //! Many places in the system code directly import this module as | |
| 14 //! a whole because it is intended to be used that way. It contains | |
| 15 //! all of the basic types needed by all system-level Mojo bindings. | |
| 16 | |
| 17 use std::fmt; | |
| 18 use std::u64; | |
| 19 use system::ffi::types::*; | |
| 20 | |
| 21 /// A MojoHandle is represented as a plain 32-bit unsigned int. | |
| 22 pub type MojoHandle = u32; | |
| 23 | |
| 24 /// Represents time ticks as specified by Mojo. A time tick value | |
| 25 /// is meaningless when not used relative to another time tick. | |
| 26 pub type MojoTimeTicks = i64; | |
| 27 | |
| 28 /// Represents a deadline for wait() calls. | |
| 29 pub type MojoDeadline = u64; | |
| 30 pub static MOJO_INDEFINITE: MojoDeadline = u64::MAX; | |
| 31 | |
| 32 pub type CreateFlags = u32; | |
| 33 pub type DuplicateFlags = u32; | |
| 34 pub type InfoFlags = u32; | |
| 35 pub type MapFlags = u32; | |
| 36 pub type WriteFlags = u32; | |
| 37 pub type ReadFlags = u32; | |
| 38 pub type AddFlags = u32; | |
| 39 | |
| 40 /// MojoResult represents anything that can happen | |
| 41 /// as a result of performing some operation in Mojo. | |
| 42 /// | |
| 43 /// It's implementation matches exactly that found in | |
| 44 /// the Mojo C API so this enum can be used across the | |
| 45 /// FFI boundary simply by using "as u32". | |
| 46 #[derive(Copy, Clone, Debug, PartialEq)] | |
| 47 #[repr(u32)] | |
| 48 pub enum MojoResult { | |
| 49 Okay = 0x0, | |
| 50 Cancelled = 0x1, | |
| 51 Unknown = 0x2, | |
| 52 InvalidArgument = 0x3, | |
| 53 DeadlineExceeded = 0x4, | |
| 54 NotFound = 0x5, | |
| 55 AlreadyExists = 0x6, | |
| 56 PermissionDenied = 0x7, | |
| 57 ResourceExhausted = 0x8, | |
| 58 FailedPrecondition = 0x9, | |
| 59 Aborted = 0xa, | |
| 60 OutOfRange = 0xb, | |
| 61 Unimplemented = 0xc, | |
| 62 Internal = 0xd, | |
| 63 Unavailable = 0xe, | |
| 64 DataLoss = 0xf, | |
| 65 Busy = 0x0019, | |
| 66 ShouldWait = 0x001e, | |
| 67 InvalidResult, | |
| 68 } | |
| 69 | |
| 70 impl MojoResult { | |
| 71 /// Convert a raw u32 code given by the C Mojo functions | |
| 72 /// into a MojoResult. | |
| 73 pub fn from_code(code: MojoResultCode) -> MojoResult { | |
| 74 match code as u32 { | |
| 75 0x0 => MojoResult::Okay, | |
| 76 0x1 => MojoResult::Cancelled, | |
| 77 0x2 => MojoResult::Unknown, | |
| 78 0x3 => MojoResult::InvalidArgument, | |
| 79 0x4 => MojoResult::DeadlineExceeded, | |
| 80 0x5 => MojoResult::NotFound, | |
| 81 0x6 => MojoResult::AlreadyExists, | |
| 82 0x7 => MojoResult::PermissionDenied, | |
| 83 0x8 => MojoResult::ResourceExhausted, | |
| 84 0x9 => MojoResult::FailedPrecondition, | |
| 85 0xa => MojoResult::Aborted, | |
| 86 0xb => MojoResult::OutOfRange, | |
| 87 0xc => MojoResult::Unimplemented, | |
| 88 0xd => MojoResult::Internal, | |
| 89 0xe => MojoResult::Unavailable, | |
| 90 0xf => MojoResult::DataLoss, | |
| 91 0x0019 => MojoResult::Busy, | |
| 92 0x001e => MojoResult::ShouldWait, | |
| 93 _ => MojoResult::InvalidResult, | |
| 94 } | |
| 95 } | |
| 96 | |
| 97 pub fn to_str(&self) -> &'static str { | |
| 98 match *self { | |
| 99 MojoResult::Okay => "OK", | |
| 100 MojoResult::Cancelled => "Cancelled", | |
| 101 MojoResult::Unknown => "Unknown", | |
| 102 MojoResult::InvalidArgument => "Invalid Argument", | |
| 103 MojoResult::DeadlineExceeded => "Deadline Exceeded", | |
| 104 MojoResult::NotFound => "Not Found", | |
| 105 MojoResult::AlreadyExists => "Already Exists", | |
| 106 MojoResult::PermissionDenied => "Permission Denied", | |
| 107 MojoResult::ResourceExhausted => "Resource Exhausted", | |
| 108 MojoResult::FailedPrecondition => "Failed Precondition", | |
| 109 MojoResult::Aborted => "Aborted", | |
| 110 MojoResult::OutOfRange => "Out Of Range", | |
| 111 MojoResult::Unimplemented => "Unimplemented", | |
| 112 MojoResult::Internal => "Internal", | |
| 113 MojoResult::Unavailable => "Unavailable", | |
| 114 MojoResult::DataLoss => "Data Loss", | |
| 115 MojoResult::Busy => "Busy", | |
| 116 MojoResult::ShouldWait => "Should Wait", | |
| 117 MojoResult::InvalidResult => "Something went very wrong", | |
| 118 } | |
| 119 } | |
| 120 } | |
| 121 | |
| 122 impl fmt::Display for MojoResult { | |
| 123 /// Allow a MojoResult to be displayed in a sane manner. | |
| 124 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
| 125 write!(f, "{}", self.to_str()) | |
| 126 } | |
| 127 } | |
| 128 | |
| 129 /// This tuple struct represents a bit vector configuration of possible | |
| 130 /// Mojo signals. Used in wait() and wait_many() primarily as a convenience. | |
| 131 /// | |
| 132 /// One invariant must be true for this data structure and it is that: | |
| 133 /// sizeof(HandleSignals) == sizeof(MojoHandleSignals) | |
| 134 /// If this is ever not the case or there is a way in Rust to ensure that, | |
| 135 /// this data structure must be updated to reflect that. | |
| 136 #[repr(C)] | |
| 137 #[derive(Clone, Copy, Default, PartialEq)] | |
| 138 pub struct HandleSignals(MojoHandleSignals); | |
| 139 | |
| 140 impl HandleSignals { | |
| 141 /// Create a new HandleSignals given the raw MojoHandleSignals | |
| 142 pub fn new(s: MojoHandleSignals) -> HandleSignals { | |
| 143 HandleSignals(s) | |
| 144 } | |
| 145 | |
| 146 /// Check if the readable flag is set | |
| 147 pub fn is_readable(&self) -> bool { | |
| 148 (self.0 & (Signals::Readable as u32)) != 0 | |
| 149 } | |
| 150 | |
| 151 /// Check if the writable flag is set | |
| 152 pub fn is_writable(&self) -> bool { | |
| 153 (self.0 & (Signals::Writable as u32)) != 0 | |
| 154 } | |
| 155 | |
| 156 /// Check if the peer-closed flag is set | |
| 157 pub fn is_peer_closed(&self) -> bool { | |
| 158 (self.0 & (Signals::PeerClosed as u32)) != 0 | |
| 159 } | |
| 160 | |
| 161 /// Check if the read threshold flag is set | |
| 162 pub fn is_read_threshold(&self) -> bool { | |
| 163 (self.0 & (Signals::ReadThreshold as u32)) != 0 | |
| 164 } | |
| 165 | |
| 166 /// Check if the write threshold flag is set | |
| 167 pub fn is_write_threshold(&self) -> bool { | |
| 168 (self.0 & (Signals::WriteThreshold as u32)) != 0 | |
| 169 } | |
| 170 | |
| 171 /// Pull the raw MojoHandleSignals out of the data structure | |
| 172 pub fn get_bits(&self) -> MojoHandleSignals { | |
| 173 self.0 | |
| 174 } | |
| 175 } | |
| 176 | |
| 177 /// Represents the signals state of a handle: which signals are satisfied, | |
| 178 /// and which are satisfiable. | |
| 179 /// | |
| 180 /// One invariant must be true for this data structure and it is that: | |
| 181 /// sizeof(SignalsState) == sizeof(MojoSignalsState) (defined in handle.h) | |
| 182 /// If this is ever not the case or there is a way in Rust to ensure that, | |
| 183 /// this data structure must be updated to reflect that. | |
| 184 #[repr(C)] | |
| 185 #[derive(Default)] | |
| 186 pub struct SignalsState { | |
| 187 satisfied: HandleSignals, | |
| 188 satisfiable: HandleSignals, | |
| 189 _align: [u32; 0], // Hack to align to a 4-byte boundary | |
| 190 } | |
| 191 | |
| 192 impl SignalsState { | |
| 193 /// Generates a new SignalsState | |
| 194 pub fn new(satisfied: HandleSignals, satisfiable: HandleSignals) -> SignalsS
tate { | |
| 195 SignalsState { | |
| 196 satisfied: satisfied, | |
| 197 satisfiable: satisfiable, | |
| 198 _align: [], | |
| 199 } | |
| 200 } | |
| 201 /// Gets a reference to the satisfied signals | |
| 202 pub fn satisfied(&self) -> &HandleSignals { | |
| 203 &self.satisfied | |
| 204 } | |
| 205 /// Gets a reference to the satisfiable signals | |
| 206 pub fn satisfiable(&self) -> &HandleSignals { | |
| 207 &self.satisfiable | |
| 208 } | |
| 209 /// Consume the SignalsState and release its tender interior | |
| 210 /// | |
| 211 /// Returns (satisfied, satisfiable) | |
| 212 pub fn unwrap(self) -> (HandleSignals, HandleSignals) { | |
| 213 (self.satisfied, self.satisfiable) | |
| 214 } | |
| 215 } | |
| 216 | |
| 217 /// The different signals options that can be | |
| 218 /// used by wait() and wait_many(). You may use | |
| 219 /// these directly to build a bit-vector, but | |
| 220 /// the signals! macro will already do it for you. | |
| 221 /// See the root of the library for more information. | |
| 222 #[repr(u32)] | |
| 223 pub enum Signals { | |
| 224 None = 0, | |
| 225 /// Wait for the handle to be readable | |
| 226 Readable = 1 << 0, | |
| 227 | |
| 228 /// Wait for the handle to be writable | |
| 229 Writable = 1 << 1, | |
| 230 | |
| 231 /// Wait for the handle to be closed by the peer | |
| 232 /// (for message pipes and data pipes, this is | |
| 233 /// the counterpart handle to the pipe) | |
| 234 PeerClosed = 1 << 2, | |
| 235 | |
| 236 /// Wait for the handle to have at least some | |
| 237 /// readable data | |
| 238 ReadThreshold = 1 << 3, | |
| 239 | |
| 240 /// Wait for the handle to allow for at least | |
| 241 /// some data to be writable | |
| 242 WriteThreshold = 1 << 4, | |
| 243 } | |
| 244 | |
| 245 /// The result struct used by the wait_set module | |
| 246 /// to return wait result information. Should remain | |
| 247 /// semantically identical to the implementation of | |
| 248 /// this struct in wait_set.h in the C bindings. | |
| 249 /// | |
| 250 /// This struct should never be constructed by anything | |
| 251 /// but the Mojo system in MojoWaitSetWait. | |
| 252 #[repr(C)] | |
| 253 pub struct WaitSetResult { | |
| 254 cookie: u64, | |
| 255 result: MojoResultCode, | |
| 256 reserved: u32, | |
| 257 signals_state: SignalsState, | |
| 258 _align: [u64; 0], // Hack to align struct to 8 byte boundary | |
| 259 } | |
| 260 | |
| 261 impl WaitSetResult { | |
| 262 /// Getter for the cookie corresponding to the handle | |
| 263 /// which just finished waiting. | |
| 264 pub fn cookie(&self) -> u64 { | |
| 265 self.cookie | |
| 266 } | |
| 267 | |
| 268 /// Getter for the wait result. | |
| 269 pub fn result(&self) -> MojoResult { | |
| 270 MojoResult::from_code(self.result) | |
| 271 } | |
| 272 | |
| 273 /// Getter for the signals state that comes with any | |
| 274 /// wait result. | |
| 275 pub fn state(&self) -> &SignalsState { | |
| 276 &self.signals_state | |
| 277 } | |
| 278 } | |
| OLD | NEW |