xref: /aosp_15_r20/bootable/libbootloader/gbl/libgbl/src/fastboot/shared.rs (revision 5225e6b173e52d2efc6bcf950c27374fd72adabc)
1*5225e6b1SAndroid Build Coastguard Worker // Copyright 2024, The Android Open Source Project
2*5225e6b1SAndroid Build Coastguard Worker //
3*5225e6b1SAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
4*5225e6b1SAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
5*5225e6b1SAndroid Build Coastguard Worker // You may obtain a copy of the License at
6*5225e6b1SAndroid Build Coastguard Worker //
7*5225e6b1SAndroid Build Coastguard Worker //     http://www.apache.org/licenses/LICENSE-2.0
8*5225e6b1SAndroid Build Coastguard Worker //
9*5225e6b1SAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*5225e6b1SAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
11*5225e6b1SAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*5225e6b1SAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
13*5225e6b1SAndroid Build Coastguard Worker // limitations under the License.
14*5225e6b1SAndroid Build Coastguard Worker 
15*5225e6b1SAndroid Build Coastguard Worker use core::{
16*5225e6b1SAndroid Build Coastguard Worker     cell::RefCell,
17*5225e6b1SAndroid Build Coastguard Worker     ops::{Deref, DerefMut},
18*5225e6b1SAndroid Build Coastguard Worker };
19*5225e6b1SAndroid Build Coastguard Worker 
20*5225e6b1SAndroid Build Coastguard Worker /// A shared instance guarded by `RefCell`.
21*5225e6b1SAndroid Build Coastguard Worker pub struct Shared<T>(RefCell<T>);
22*5225e6b1SAndroid Build Coastguard Worker 
23*5225e6b1SAndroid Build Coastguard Worker impl<T> From<T> for Shared<T> {
from(val: T) -> Self24*5225e6b1SAndroid Build Coastguard Worker     fn from(val: T) -> Self {
25*5225e6b1SAndroid Build Coastguard Worker         Shared(val.into())
26*5225e6b1SAndroid Build Coastguard Worker     }
27*5225e6b1SAndroid Build Coastguard Worker }
28*5225e6b1SAndroid Build Coastguard Worker 
29*5225e6b1SAndroid Build Coastguard Worker impl<T> Deref for Shared<T> {
30*5225e6b1SAndroid Build Coastguard Worker     type Target = RefCell<T>;
31*5225e6b1SAndroid Build Coastguard Worker 
deref(&self) -> &Self::Target32*5225e6b1SAndroid Build Coastguard Worker     fn deref(&self) -> &Self::Target {
33*5225e6b1SAndroid Build Coastguard Worker         &self.0
34*5225e6b1SAndroid Build Coastguard Worker     }
35*5225e6b1SAndroid Build Coastguard Worker }
36*5225e6b1SAndroid Build Coastguard Worker 
37*5225e6b1SAndroid Build Coastguard Worker impl<T> DerefMut for Shared<T> {
deref_mut(&mut self) -> &mut Self::Target38*5225e6b1SAndroid Build Coastguard Worker     fn deref_mut(&mut self) -> &mut Self::Target {
39*5225e6b1SAndroid Build Coastguard Worker         &mut self.0
40*5225e6b1SAndroid Build Coastguard Worker     }
41*5225e6b1SAndroid Build Coastguard Worker }
42