sandbox/
instance_token.rs

1// Copyright 2024 The Fuchsia 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
5use fidl_fuchsia_component_sandbox as fsandbox;
6use std::any::Any;
7use std::fmt::Debug;
8use std::sync::Arc;
9
10/// The trait that `WeakInstanceToken` holds.
11pub trait WeakInstanceTokenAny: Debug + Send + Sync {
12    fn as_any(&self) -> &dyn Any;
13}
14
15/// A type representing a weak pointer to a component.
16/// This is type erased because the bedrock library shouldn't depend on
17/// Component Manager types.
18#[derive(Clone, Debug)]
19pub struct WeakInstanceToken {
20    pub inner: Arc<dyn WeakInstanceTokenAny>,
21}
22
23impl From<WeakInstanceToken> for fsandbox::Capability {
24    fn from(_component: WeakInstanceToken) -> Self {
25        todo!("b/337284929: Decide on if InstanceToken should be in Capability");
26    }
27}
28
29impl WeakInstanceToken {
30    /// Creates a new WeakInstanceToken that cannot be typecast into anything useful. Primarily
31    /// useful in tests.
32    pub fn new_invalid() -> Self {
33        #[derive(Debug)]
34        struct Nothing;
35        impl WeakInstanceTokenAny for Nothing {
36            fn as_any(&self) -> &dyn Any {
37                self
38            }
39        }
40        Self { inner: Arc::new(Nothing {}) }
41    }
42}