Skip to content

Commit 4b2d83c

Browse files
code refactor
1 parent db95eb1 commit 4b2d83c

File tree

8 files changed

+121
-115
lines changed

8 files changed

+121
-115
lines changed

lib/ffi.dart

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import 'dart:ffi'; // For FFI
2-
import 'dart:io'; // For Platform.isX
1+
import 'dart:ffi' show DynamicLibrary; // For FFI
2+
import 'dart:io' show Platform; // For Platform.isX
33

44
late DynamicLibrary dynamicLib;
55

@@ -13,16 +13,3 @@ void initDynamicLib() {
1313
)
1414
: DynamicLibrary.process();
1515
}
16-
17-
typedef PrintSomethingC = Void Function();
18-
typedef PrintSomethingDart = void Function();
19-
20-
final printSomething =
21-
dynamicLib.lookupFunction<PrintSomethingC, PrintSomethingDart>(
22-
'print_something'); //? print_something is a function name of rust code.
23-
24-
typedef SumTwoNumbersC = Double Function(Double x, Double y);
25-
typedef SumTwoNumbersDart = double Function(double x, double y);
26-
27-
final sumTwoNumbers = dynamicLib
28-
.lookupFunction<SumTwoNumbersC, SumTwoNumbersDart>('sum_two_numbers');

lib/main.dart

Lines changed: 2 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
import 'dart:developer';
2-
31
import 'package:flutter/material.dart';
42
import 'package:flutter_riverpod/flutter_riverpod.dart';
53

6-
import 'ffi.dart' show initDynamicLib, printSomething, sumTwoNumbers;
4+
import 'ffi.dart' show initDynamicLib;
75

8-
import 'stream/provider.dart';
9-
import 'string_ffi.dart';
6+
import 'src/module/home.dart' show HomeScreen;
107

118
void main() {
129
initDynamicLib();
@@ -16,77 +13,3 @@ void main() {
1613
),
1714
));
1815
}
19-
20-
class HomeScreen extends StatelessWidget {
21-
const HomeScreen({super.key});
22-
23-
@override
24-
Widget build(BuildContext context) {
25-
return Scaffold(
26-
body: Column(
27-
mainAxisAlignment: MainAxisAlignment.center,
28-
crossAxisAlignment: CrossAxisAlignment.center,
29-
children: [
30-
ElevatedButton(
31-
onPressed: () {
32-
final start = DateTime.now();
33-
_time();
34-
log(DateTime.now().difference(start).toString());
35-
},
36-
child: const Text('Dart Loop'),
37-
),
38-
const SizedBox(width: double.infinity, height: 10),
39-
ElevatedButton(
40-
onPressed: () {
41-
final start = DateTime.now();
42-
printSomething();
43-
log(DateTime.now().difference(start).toString());
44-
},
45-
child: const Text('Rust Loop'),
46-
),
47-
const SizedBox(width: double.infinity, height: 10),
48-
ElevatedButton(
49-
onPressed: () {
50-
final sum = sumTwoNumbers(10.0, 30.0);
51-
log('Sum : $sum');
52-
},
53-
child: const Text('Calculate Sum'),
54-
),
55-
const SizedBox(width: double.infinity, height: 10),
56-
ElevatedButton(
57-
onPressed: () {
58-
final profile = concatStringUsingRust('Remon Ahammad', 23);
59-
log('Profile : $profile');
60-
},
61-
child: const Text('Profile Concat in Rust code'),
62-
),
63-
const SizedBox(width: double.infinity, height: 10),
64-
Consumer(builder: (context, state, __) {
65-
state.watch(startRustProvider);
66-
final eventData = state.watch(rustEventProvider);
67-
68-
return eventData.when(
69-
error: (e, _) => Text(e.toString()),
70-
loading: () => const CircularProgressIndicator(),
71-
data: (data) => Text(String.fromCharCodes(data)),
72-
);
73-
// return ElevatedButton(
74-
// onPressed: () async {
75-
// // startRustStream();
76-
// },
77-
// child: const Text('Stream'),
78-
// );
79-
}),
80-
],
81-
),
82-
);
83-
}
84-
85-
_time() {
86-
int x = 0;
87-
for (int i = 0; i < 1000000000; i++) {
88-
x += i;
89-
}
90-
log('Sum : $x');
91-
}
92-
}

lib/string_ffi.dart renamed to lib/src/ffi/concat/string_ffi.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import 'dart:ffi';
22
import 'package:ffi/ffi.dart';
3-
import 'ffi.dart';
3+
import '../../../ffi.dart';
44

55
typedef PCC = Pointer<Utf8> Function(Pointer<Uint8> name, Int32 age);
66
typedef PCDart = Pointer<Utf8> Function(Pointer<Uint8> name, int age);

lib/stream/provider.dart renamed to lib/src/ffi/stream/provider.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import 'dart:async';
2-
import 'dart:typed_data';
1+
import 'dart:async' show FutureOr;
2+
import 'dart:typed_data' show Uint8List;
33

4-
import 'package:flutter_riverpod/flutter_riverpod.dart';
4+
import 'package:flutter_riverpod/flutter_riverpod.dart'
5+
show AsyncNotifier, AsyncNotifierProvider, StreamProvider;
56

6-
import '../stream.dart';
7-
import 'thread.dart';
7+
import 'thread.dart' show startThread, streamController;
88

99
final rustEventProvider =
1010
StreamProvider<Uint8List>((ref) => streamController.stream);
Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
import 'dart:developer';
2-
import 'dart:ffi'; // For FFI
3-
import 'dart:typed_data'; // For Uint8List
4-
import 'dart:async';
1+
import 'dart:ffi'
2+
show IntPtr, NativeFunction, NativeFunctionPointer, Pointer, Uint8, Void;
53

6-
import 'package:flutter_riverpod/flutter_riverpod.dart';
7-
8-
import 'ffi.dart'; // For streams
4+
import '../../../ffi.dart' show dynamicLib;
95

106
typedef StreamCallback = Void Function(Pointer<Uint8>, IntPtr);
117

@@ -15,4 +11,3 @@ typedef StartStreamNative = Void Function(
1511
final startStream = dynamicLib
1612
.lookup<NativeFunction<StartStreamNative>>('start_stream')
1713
.asFunction<void Function(Pointer<NativeFunction<StreamCallback>>)>();
18-

lib/stream/thread.dart renamed to lib/src/ffi/stream/thread.dart

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import 'dart:async';
2-
import 'dart:developer';
3-
import 'dart:ffi';
4-
import 'dart:isolate';
5-
import 'dart:typed_data';
1+
import 'dart:async' show StreamController;
2+
import 'dart:developer' show log;
3+
import 'dart:ffi' show Pointer, Uint8, Uint8Pointer;
4+
import 'dart:isolate' show Isolate, ReceivePort, SendPort;
5+
import 'dart:typed_data' show Uint8List;
66

7-
import '../ffi.dart';
8-
import '../stream.dart';
7+
import '../../../ffi.dart' show initDynamicLib;
8+
import 'stream.dart' show StreamCallback, startStream;
99

1010
late SendPort sendPort;
1111
final streamController = StreamController<Uint8List>();

lib/src/ffi/sum/sum.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import 'dart:ffi' show Double, DynamicLibraryExtension, Void;
2+
3+
import '../../../ffi.dart' show dynamicLib;
4+
5+
///? loop
6+
typedef PrintSomethingC = Void Function();
7+
typedef PrintSomethingDart = void Function();
8+
9+
final printSomething =
10+
dynamicLib.lookupFunction<PrintSomethingC, PrintSomethingDart>(
11+
'print_something'); //? print_something is a function name of rust code.
12+
13+
///? sum
14+
typedef SumTwoNumbersC = Double Function(Double x, Double y);
15+
typedef SumTwoNumbersDart = double Function(double x, double y);
16+
17+
final sumTwoNumbers = dynamicLib
18+
.lookupFunction<SumTwoNumbersC, SumTwoNumbersDart>('sum_two_numbers');

lib/src/module/home.dart

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import 'dart:developer' show log;
2+
3+
import 'package:flutter/material.dart';
4+
import 'package:flutter_riverpod/flutter_riverpod.dart'
5+
show AsyncValueX, Consumer;
6+
7+
import '../ffi/concat/string_ffi.dart' show concatStringUsingRust;
8+
import '../ffi/stream/provider.dart' show rustEventProvider, startRustProvider;
9+
import '../ffi/sum/sum.dart' show printSomething, sumTwoNumbers;
10+
11+
class HomeScreen extends StatelessWidget {
12+
const HomeScreen({super.key});
13+
14+
@override
15+
Widget build(BuildContext context) {
16+
return Scaffold(
17+
body: Column(
18+
mainAxisAlignment: MainAxisAlignment.center,
19+
crossAxisAlignment: CrossAxisAlignment.center,
20+
children: [
21+
ElevatedButton(
22+
onPressed: () {
23+
final start = DateTime.now();
24+
_time();
25+
log(DateTime.now().difference(start).toString());
26+
},
27+
child: const Text('Dart Loop'),
28+
),
29+
const SizedBox(width: double.infinity, height: 10),
30+
ElevatedButton(
31+
onPressed: () {
32+
final start = DateTime.now();
33+
printSomething();
34+
log(DateTime.now().difference(start).toString());
35+
},
36+
child: const Text('Rust Loop'),
37+
),
38+
const SizedBox(width: double.infinity, height: 10),
39+
ElevatedButton(
40+
onPressed: () {
41+
final sum = sumTwoNumbers(10.0, 30.0);
42+
log('Sum : $sum');
43+
},
44+
child: const Text('Calculate Sum'),
45+
),
46+
const SizedBox(width: double.infinity, height: 10),
47+
ElevatedButton(
48+
onPressed: () {
49+
final profile = concatStringUsingRust('Remon Ahammad', 23);
50+
log('Profile : $profile');
51+
},
52+
child: const Text('Profile Concat in Rust code'),
53+
),
54+
const SizedBox(width: double.infinity, height: 10),
55+
Consumer(builder: (context, state, __) {
56+
state.watch(startRustProvider);
57+
final eventData = state.watch(rustEventProvider);
58+
59+
return eventData.when(
60+
error: (e, _) => Text(e.toString()),
61+
loading: () => const CircularProgressIndicator(),
62+
data: (data) => Text(String.fromCharCodes(data)),
63+
);
64+
// return ElevatedButton(
65+
// onPressed: () async {
66+
// // startRustStream();
67+
// },
68+
// child: const Text('Stream'),
69+
// );
70+
}),
71+
],
72+
),
73+
);
74+
}
75+
76+
_time() {
77+
int x = 0;
78+
for (int i = 0; i < 1000000000; i++) {
79+
x += i;
80+
}
81+
log('Sum : $x');
82+
}
83+
}

0 commit comments

Comments
 (0)