| 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 //! The way Mojo handles are handled in Rust is very similar | |
| 6 //! to Go, though more type-safe. Here we define an "untyped" | |
| 7 //! handle which means that what the handle actually represents | |
| 8 //! is unknown. This is the basic handle wrapper, and it handles | |
| 9 //! closing the handle once the wrapper goes out of scope, therefore | |
| 10 //! preventing any resources from being leaked. "Typed" handles | |
| 11 //! are MessageEndpoints or Consumers/Producers in this library | |
| 12 //! and they represent handles which represent parts of message pipes, | |
| 13 //! data pipes, and shared buffers. Typed handles wrap untyped handles | |
| 14 //! but act much the same as untyped handles. | |
| 15 | |
| 16 use system::ffi; | |
| 17 // This full import is intentional; nearly every type in mojo_types needs to be
used. | |
| 18 use system::mojo_types::*; | |
| 19 | |
| 20 /// The CastHandle trait defines an interface to convert between | |
| 21 /// typed and untyped handles. These are only used internally for | |
| 22 /// typed handles. | |
| 23 pub trait CastHandle { | |
| 24 /// Passes an ownership of an untyped handle and produces a | |
| 25 /// typed handle which owns that untyped handle | |
| 26 /// | |
| 27 /// Casting to a typed handle is unsafe because the handle may | |
| 28 /// not necessarily be a handle to the typed primitive being | |
| 29 /// casted to internally in Mojo | |
| 30 unsafe fn from_untyped(handle: UntypedHandle) -> Self; | |
| 31 | |
| 32 /// Consumes a typed handle and releases ownership of the | |
| 33 /// untyped handle it owned | |
| 34 fn as_untyped(self) -> UntypedHandle; | |
| 35 } | |
| 36 | |
| 37 /// The Handle trait simply means that we can extract | |
| 38 /// the native integer-based Mojo handle from the typed handle. | |
| 39 pub trait Handle { | |
| 40 /// Returns the native handle wrapped by whatever structure | |
| 41 /// implements this trait. | |
| 42 fn get_native_handle(&self) -> MojoHandle; | |
| 43 | |
| 44 /// Waits on the handle wrapped in the current struct until the signals | |
| 45 /// declared in 'signals' are triggered, waiting for a maximum time of | |
| 46 /// 'deadline'. This method blocks. | |
| 47 /// | |
| 48 /// Returns the satisfied and satisfiable signals respectively for this | |
| 49 /// handle when waiting is done. | |
| 50 fn wait(&self, signals: HandleSignals, deadline: MojoDeadline) -> (SignalsSt
ate, MojoResult) { | |
| 51 let mut state: SignalsState = Default::default(); | |
| 52 let r = unsafe { | |
| 53 ffi::MojoWait(self.get_native_handle(), | |
| 54 signals, | |
| 55 deadline, | |
| 56 &mut state as *mut SignalsState) | |
| 57 }; | |
| 58 (state, MojoResult::from_code(r)) | |
| 59 } | |
| 60 } | |
| 61 | |
| 62 /// The basic untyped handle that wraps a MojoHandle (a u32) | |
| 63 pub struct UntypedHandle { | |
| 64 /// The native Mojo handle | |
| 65 value: MojoHandle, | |
| 66 } | |
| 67 | |
| 68 impl UntypedHandle { | |
| 69 /// Invalidates the Handle by setting its native handle to | |
| 70 /// zero, the canonical invalid handle in Mojo. | |
| 71 /// | |
| 72 /// This function is unsafe because clearing a native handle | |
| 73 /// without closing it is a resource leak. | |
| 74 pub unsafe fn invalidate(&mut self) { | |
| 75 self.value = 0 | |
| 76 } | |
| 77 | |
| 78 /// Checks if the native handle is valid (0 = canonical invalid handle). | |
| 79 pub fn is_valid(&self) -> bool { | |
| 80 self.value != 0 | |
| 81 } | |
| 82 } | |
| 83 | |
| 84 impl Handle for UntypedHandle { | |
| 85 /// Pulls out a copy of the native handle wrapped by this structure. | |
| 86 fn get_native_handle(&self) -> MojoHandle { | |
| 87 self.value | |
| 88 } | |
| 89 } | |
| 90 | |
| 91 impl CastHandle for UntypedHandle { | |
| 92 /// Casting an untyped handle is a no-op, but we include | |
| 93 /// this to eliminate code duplication. | |
| 94 unsafe fn from_untyped(handle: UntypedHandle) -> Self { | |
| 95 handle | |
| 96 } | |
| 97 | |
| 98 /// Casting to an untyped handle is a no-op, but we include | |
| 99 /// this to eliminate code duplication. | |
| 100 fn as_untyped(self) -> UntypedHandle { | |
| 101 self | |
| 102 } | |
| 103 } | |
| 104 | |
| 105 impl Drop for UntypedHandle { | |
| 106 /// The destructor for an untyped handle which closes the native handle | |
| 107 /// it wraps. | |
| 108 fn drop(&mut self) { | |
| 109 if self.is_valid() { | |
| 110 let result = MojoResult::from_code(unsafe { ffi::MojoClose(self.get_
native_handle()) }); | |
| 111 if result != MojoResult::Okay { | |
| 112 panic!("Failed to close handle! Reason: {}", result); | |
| 113 } | |
| 114 } | |
| 115 } | |
| 116 } | |
| 117 | |
| 118 /// Acquires a native handle by wrapping it in an untyped handle, allowing | |
| 119 /// us to track the resource and free it appropriately | |
| 120 pub unsafe fn acquire(handle: MojoHandle) -> UntypedHandle { | |
| 121 UntypedHandle { value: handle } | |
| 122 } | |
| OLD | NEW |