Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/src/rust_lib_init.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// SPDX-FileCopyrightText: 2026 Foundation Devices Inc.
//
// SPDX-License-Identifier: MIT

import 'rust/frb_generated.dart';

Future<void> ensureRustLibInit() async {
if (!RustLib.instance.initialized) {
await RustLib.init();
}
}
19 changes: 3 additions & 16 deletions lib/tor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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.
///
Expand All @@ -100,13 +97,7 @@ class Tor {
static Future<Tor> 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;
}

Expand Down Expand Up @@ -158,11 +149,7 @@ class Tor {
Future<void> 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();
Expand Down
3 changes: 3 additions & 0 deletions lib/util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
// SPDX-License-Identifier: MIT

import 'src/rust/api/tor.dart' as rust;
import 'src/rust_lib_init.dart';

Future<int> getNofileLimit() async {
await ensureRustLibInit();
final limit = await rust.getNofileLimit();
return limit.toInt();
}

Future<int> setNofileLimit(int limit) async {
await ensureRustLibInit();
final result = await rust.setNofileLimit(limit: BigInt.from(limit));
return result.toInt();
}
Loading