Skip to main content

runtime_capabilities/
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 std::any::Any;
6use std::fmt::Debug;
7use std::sync::Arc;
8
9/// The trait that `WeakInstanceToken` holds.
10pub trait WeakInstanceTokenAny: Debug + Send + Sync {
11    fn as_any(&self) -> &dyn Any;
12}
13
14/// A type representing a weak pointer to a component.
15/// This is type erased because the bedrock library shouldn't depend on
16/// Component Manager types.
17#[derive(Debug)]
18pub struct WeakInstanceToken {
19    pub inner: Box<dyn WeakInstanceTokenAny>,
20}
21
22impl WeakInstanceToken {
23    /// Creates a new WeakInstanceToken that cannot be typecast into anything useful. Primarily
24    /// useful in tests.
25    pub fn new_invalid() -> Arc<Self> {
26        #[derive(Debug)]
27        struct Nothing;
28        impl WeakInstanceTokenAny for Nothing {
29            fn as_any(&self) -> &dyn Any {
30                self
31            }
32        }
33        Arc::new(Self { inner: Box::new(Nothing {}) })
34    }
35
36    #[cfg(target_os = "fuchsia")]
37    pub fn try_into_directory_entry(
38        self: Arc<Self>,
39        _scope: vfs::execution_scope::ExecutionScope,
40        _token: Arc<crate::WeakInstanceToken>,
41    ) -> Result<Arc<dyn vfs::directory::entry::DirectoryEntry>, crate::ConversionError> {
42        Err(crate::ConversionError::NotSupported)
43    }
44}