From a90a5cba08fdf6c0ceb95101c275108a152a3aed Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 12 Jun 2024 07:35:37 +0200 Subject: Merging upstream version 127.0. Signed-off-by: Daniel Baumann --- .../uniffi-fixtures/refcounts/src/lib.rs | 32 ++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 toolkit/components/uniffi-fixtures/refcounts/src/lib.rs (limited to 'toolkit/components/uniffi-fixtures/refcounts/src/lib.rs') diff --git a/toolkit/components/uniffi-fixtures/refcounts/src/lib.rs b/toolkit/components/uniffi-fixtures/refcounts/src/lib.rs new file mode 100644 index 0000000000..6554625c62 --- /dev/null +++ b/toolkit/components/uniffi-fixtures/refcounts/src/lib.rs @@ -0,0 +1,32 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +/// This crate exists to test managing the Rust Arc strong counts as JS objects are +/// created/destroyed. See `test_refcounts.js` for how it's used. +use std::sync::{Arc, Mutex}; + +pub struct SingletonObject; + +impl SingletonObject { + pub fn method(&self) {} +} + +static SINGLETON: Mutex>> = Mutex::new(None); + +pub fn get_singleton() -> Arc { + Arc::clone( + SINGLETON + .lock() + .unwrap() + .get_or_insert_with(|| Arc::new(SingletonObject)), + ) +} + +pub fn get_js_refcount() -> i32 { + // Subtract 2: one for the reference in the Mutex and one for the temporary reference that + // we're calling Arc::strong_count on. + (Arc::strong_count(&get_singleton()) as i32) - 2 +} + +include!(concat!(env!("OUT_DIR"), "/refcounts.uniffi.rs")); -- cgit v1.2.3