Skip to content

Commit 6013cb7

Browse files
authored
Switch to package:dart_flutter_team_lints (#1272)
--- <details> <summary>Contribution guidelines:</summary><br> - See our [contributor guide](https://github.com/dart-lang/.github/blob/main/CONTRIBUTING.md) for general expectations for PRs. - Larger or significant changes should be discussed in an issue before creating a PR. - Contributions to our repos should follow the [Dart style guide](https://dart.dev/guides/language/effective-dart) and use `dart format`. - Most changes should add an entry to the changelog and may need to [rev the pubspec package version](https://github.com/dart-lang/sdk/blob/main/docs/External-Package-Maintenance.md#making-a-change). - Changes to packages require [corresponding tests](https://github.com/dart-lang/.github/blob/main/CONTRIBUTING.md#Testing). Note that many Dart repos have a weekly cadence for reviewing PRs - please allow for some latency before initial review feedback. </details>
1 parent b054170 commit 6013cb7

File tree

13 files changed

+104
-107
lines changed

13 files changed

+104
-107
lines changed

pkgs/ffi/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.1.3-wip
2+
3+
- Use `package:dart_flutter_team_lints`.
4+
15
## 2.1.2
26

37
- Update repository to point to dart-lang/native.

pkgs/ffi/analysis_options.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
include: package:lints/recommended.yaml
1+
include: package:dart_flutter_team_lints/analysis_options.yaml
22

33
analyzer:
44
language:
55
strict-casts: true
66
strict-inference: true
7+
8+
linter:
9+
rules:
10+
- prefer_final_locals

pkgs/ffi/example/main.dart

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
15
import 'dart:ffi';
26

37
import 'package:ffi/ffi.dart';
@@ -9,9 +13,10 @@ void main() {
913
print(pointer.value);
1014
calloc.free(pointer);
1115

12-
// Use the Utf8 helper to encode zero-terminated UTF-8 strings in native memory.
13-
final String myString = '😎👿💬';
14-
final Pointer<Utf8> charPointer = myString.toNativeUtf8();
16+
// Use the Utf8 helper to encode zero-terminated UTF-8 strings in native
17+
// memory.
18+
final myString = '😎👿💬';
19+
final charPointer = myString.toNativeUtf8();
1520
print('First byte is: ${charPointer.cast<Uint8>().value}');
1621
print(charPointer.toDartString());
1722
calloc.free(charPointer);

pkgs/ffi/lib/ffi.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44

55
export 'src/allocation.dart' show calloc, malloc;
66
export 'src/arena.dart';
7-
export 'src/utf8.dart';
87
export 'src/utf16.dart';
8+
export 'src/utf8.dart';

pkgs/ffi/lib/src/allocation.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ final class MallocAllocator implements Allocator {
8686

8787
/// Returns a pointer to a native free function.
8888
///
89-
/// This function can be used to release memory allocated by [allocated]
89+
/// This function can be used to release memory allocated by [allocate]
9090
/// from the native side. It can also be used as a finalization callback
9191
/// passed to `NativeFinalizer` constructor or `Pointer.atTypedList`
9292
/// method.
@@ -189,7 +189,7 @@ final class CallocAllocator implements Allocator {
189189

190190
/// Returns a pointer to a native free function.
191191
///
192-
/// This function can be used to release memory allocated by [allocated]
192+
/// This function can be used to release memory allocated by [allocate]
193193
/// from the native side. It can also be used as a finalization callback
194194
/// passed to `NativeFinalizer` constructor or `Pointer.atTypedList`
195195
/// method.

pkgs/ffi/lib/src/arena.dart

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import 'dart:async';
88
import 'dart:ffi';
99

10-
import 'package:ffi/ffi.dart';
10+
import '../ffi.dart';
1111

1212
/// An [Allocator] which frees all allocations at the same time.
1313
///
@@ -119,12 +119,12 @@ class Arena implements Allocator {
119119
R using<R>(R Function(Arena) computation,
120120
[Allocator wrappedAllocator = calloc]) {
121121
final arena = Arena(wrappedAllocator);
122-
bool isAsync = false;
122+
var isAsync = false;
123123
try {
124124
final result = computation(arena);
125125
if (result is Future) {
126126
isAsync = true;
127-
return (result.whenComplete(arena.releaseAll) as R);
127+
return result.whenComplete(arena.releaseAll) as R;
128128
}
129129
return result;
130130
} finally {
@@ -143,16 +143,14 @@ R using<R>(R Function(Arena) computation,
143143
R withZoneArena<R>(R Function() computation,
144144
[Allocator wrappedAllocator = calloc]) {
145145
final arena = Arena(wrappedAllocator);
146-
var arenaHolder = [arena];
147-
bool isAsync = false;
146+
final arenaHolder = [arena];
147+
var isAsync = false;
148148
try {
149149
return runZoned(() {
150150
final result = computation();
151151
if (result is Future) {
152152
isAsync = true;
153-
return result.whenComplete(() {
154-
arena.releaseAll();
155-
}) as R;
153+
return result.whenComplete(arena.releaseAll) as R;
156154
}
157155
return result;
158156
}, zoneValues: {#_arena: arenaHolder});

pkgs/ffi/lib/src/utf16.dart

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'dart:ffi';
6-
import 'dart:typed_data';
76

8-
import 'package:ffi/ffi.dart';
7+
import '../ffi.dart';
98

109
/// The contents of a native zero-terminated array of UTF-16 code units.
1110
///
@@ -92,8 +91,8 @@ extension StringUtf16Pointer on String {
9291
/// Returns an [allocator]-allocated pointer to the result.
9392
Pointer<Utf16> toNativeUtf16({Allocator allocator = malloc}) {
9493
final units = codeUnits;
95-
final Pointer<Uint16> result = allocator<Uint16>(units.length + 1);
96-
final Uint16List nativeString = result.asTypedList(units.length + 1);
94+
final result = allocator<Uint16>(units.length + 1);
95+
final nativeString = result.asTypedList(units.length + 1);
9796
nativeString.setRange(0, units.length, units);
9897
nativeString[units.length] = 0;
9998
return result.cast();

pkgs/ffi/lib/src/utf8.dart

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44

55
import 'dart:convert';
66
import 'dart:ffi';
7-
import 'dart:typed_data';
87

9-
import 'package:ffi/ffi.dart';
8+
import '../ffi.dart';
109

1110
/// The contents of a native zero-terminated array of UTF-8 code units.
1211
///
@@ -80,8 +79,8 @@ extension StringUtf8Pointer on String {
8079
/// Returns an [allocator]-allocated pointer to the result.
8180
Pointer<Utf8> toNativeUtf8({Allocator allocator = malloc}) {
8281
final units = utf8.encode(this);
83-
final Pointer<Uint8> result = allocator<Uint8>(units.length + 1);
84-
final Uint8List nativeString = result.asTypedList(units.length + 1);
82+
final result = allocator<Uint8>(units.length + 1);
83+
final nativeString = result.asTypedList(units.length + 1);
8584
nativeString.setAll(0, units);
8685
nativeString[units.length] = 0;
8786
return result.cast();

pkgs/ffi/pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: ffi
2-
version: 2.1.2
2+
version: 2.1.3-wip
33
description: Utilities for working with Foreign Function Interface (FFI) code.
44
repository: https://github.com/dart-lang/native/tree/main/pkgs/ffi
55

@@ -12,5 +12,5 @@ environment:
1212
sdk: '>=3.3.0-279.1.beta <4.0.0'
1313

1414
dev_dependencies:
15+
dart_flutter_team_lints: ^2.0.0
1516
test: ^1.21.2
16-
lints: ^2.0.0

pkgs/ffi/test/allocation_test.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,19 @@ void main() async {
1616
for (var i = 0; i < testRuns; i++) {
1717
final allocBytes = Random().nextInt(1000);
1818
final mem = calloc<Uint8>(allocBytes);
19-
expect(mem.asTypedList(allocBytes).where(((element) => element != 0)),
19+
expect(mem.asTypedList(allocBytes).where((element) => element != 0),
2020
isEmpty);
2121
calloc.free(mem);
2222
}
2323
});
2424

2525
test('testPointerAllocateTooLarge', () {
2626
// Try to allocate something that doesn't fit in 64 bit address space.
27-
int maxInt = 9223372036854775807; // 2^63 - 1
27+
final maxInt = 9223372036854775807; // 2^63 - 1
2828
expect(() => calloc<Uint8>(maxInt), throwsA(isA<ArgumentError>()));
2929

3030
// Try to allocate almost the full 64 bit address space.
31-
int maxInt1_8 = 1152921504606846975; // 2^60 -1
31+
final maxInt1_8 = 1152921504606846975; // 2^60 -1
3232
expect(() => calloc<Uint8>(maxInt1_8), throwsA(isA<ArgumentError>()));
3333
});
3434

0 commit comments

Comments
 (0)