diff --git a/lib/src/rust_lib_init.dart b/lib/src/rust_lib_init.dart new file mode 100644 index 0000000..0293178 --- /dev/null +++ b/lib/src/rust_lib_init.dart @@ -0,0 +1,11 @@ +// SPDX-FileCopyrightText: 2026 Foundation Devices Inc. +// +// SPDX-License-Identifier: MIT + +import 'rust/frb_generated.dart'; + +Future ensureRustLibInit() async { + if (!RustLib.instance.initialized) { + await RustLib.init(); + } +} diff --git a/lib/tor.dart b/lib/tor.dart index 4ea83af..c1c9c1e 100644 --- a/lib/tor.dart +++ b/lib/tor.dart @@ -10,8 +10,8 @@ import 'package:flutter/foundation.dart'; import 'package:flutter_rust_bridge/flutter_rust_bridge_for_generated.dart'; import 'package:path_provider/path_provider.dart'; -import 'src/rust/frb_generated.dart'; import 'src/rust/api/tor.dart' as rust; +import 'src/rust_lib_init.dart'; export 'src/rust/api/tor.dart' show TorError; @@ -88,9 +88,6 @@ class Tor { /// Getter for the singleton instance of the Tor class. static Tor get instance => _instance; - /// Whether RustLib has been initialized. - static bool _rustLibInitialized = false; - /// Initialize the Tor ffi lib instance if it hasn't already been set. Nothing /// changes if _tor is already been set. /// @@ -100,13 +97,7 @@ class Tor { static Future init({bool enabled = true}) async { var singleton = Tor._instance; singleton._enabled = enabled; - - // Initialize RustLib if not already done - if (!_rustLibInitialized) { - await RustLib.init(); - _rustLibInitialized = true; - } - + await ensureRustLibInit(); return singleton; } @@ -158,11 +149,7 @@ class Tor { Future start() async { broadcastState(); - // Ensure RustLib is initialized - if (!_rustLibInitialized) { - await RustLib.init(); - _rustLibInitialized = true; - } + await ensureRustLibInit(); // Set the state and cache directories. final Directory appSupportDir = await getApplicationSupportDirectory(); diff --git a/lib/util.dart b/lib/util.dart index 6fd3a71..a56e589 100644 --- a/lib/util.dart +++ b/lib/util.dart @@ -3,13 +3,16 @@ // SPDX-License-Identifier: MIT import 'src/rust/api/tor.dart' as rust; +import 'src/rust_lib_init.dart'; Future getNofileLimit() async { + await ensureRustLibInit(); final limit = await rust.getNofileLimit(); return limit.toInt(); } Future setNofileLimit(int limit) async { + await ensureRustLibInit(); final result = await rust.setNofileLimit(limit: BigInt.from(limit)); return result.toInt(); }