Skip to content

Commit 34b1851

Browse files
authored
Just use int. (#51)
* Just use int. * ++
1 parent b99516a commit 34b1851

12 files changed

+221
-777
lines changed

CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@
99
1010
[file an issue]: https://github.com/matanlurey/binary.dart/issues
1111

12+
## 4.0.0-alpha+2
13+
14+
**Breaking changes**:
15+
16+
- `<FixedInt>`, which was pointless, is now just `int`. That means that any
17+
fixed integer representation can be provided somewhere an `int` is expected,
18+
which cuts down on boilerplate without much value.
19+
1220
## 4.0.0-alpha+1
1321

1422
**New features**:

lib/src/descriptor.dart

-14
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,6 @@ const debugCheckUncheckedInRange = bool.fromEnvironment(
2727
/// Whether the platform is JavaScript.
2828
const _isJsNumerics = identical(1, 1.0);
2929

30-
/// A base extension type for fixed-width integers.
31-
///
32-
/// This type is not very useful, and mostly exists for consistency.
33-
///
34-
/// @nodoc
35-
extension type const FixedInt._(int _) implements Comparable<num> {
36-
/// This number as an [int].
37-
///
38-
/// This is the underlying integer representation of the `FixedInt`, and is
39-
/// effectively an identity function, but for consistency and completeness,
40-
/// it is provided as a method to discourage casting.
41-
int toInt() => _;
42-
}
43-
4430
/// A descriptor for a fixed-width integer type [T].
4531
///
4632
/// An integer descriptor is used to describe the properties of a fixed-width

lib/src/int16.dart

+26-111
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ import 'package:meta/meta.dart';
6262
/// important to be aware of this limitation.
6363
///
6464
/// This also applies to methods such as [List.cast] or [Iterable.whereType].
65-
extension type const Int16._(int _) implements FixedInt {
65+
extension type const Int16._(int _) implements int {
6666
static const _descriptor = IntDescriptor<Int16>.signed(
6767
Int16.fromUnchecked,
6868
width: width,
@@ -72,6 +72,9 @@ extension type const Int16._(int _) implements FixedInt {
7272
/// Always `0`.
7373
static const zero = Int16.fromUnchecked(0);
7474

75+
/// Always `1`.
76+
static const one = Int16.fromUnchecked(1);
77+
7578
/// The minimum value that this type can represent.
7679
static const min = Int16.fromUnchecked(-32768);
7780

@@ -157,21 +160,6 @@ extension type const Int16._(int _) implements FixedInt {
157160
@pragma('vm:prefer-inline')
158161
factory Int16.fromClamped(int v) => _descriptor.fitClamping(v);
159162

160-
/// Defines an existing fixed-width integer [v] as a Int16.
161-
///
162-
/// - If `this`'s width is >= [v]'s width, the result is the same as [v].
163-
/// - Otherwise, the result is clamped to fit, similar to `fromClamped`.
164-
///
165-
/// This is a convenience constructor; similar behavior can be achieved with:
166-
///
167-
/// ```dart
168-
/// final rawInt = v.toInt();
169-
/// Int16.fromClamped(rawInt);
170-
/// ```
171-
@pragma('dart2js:tryInline')
172-
@pragma('vm:prefer-inline')
173-
factory Int16.fromInt(FixedInt v) => _descriptor.fitClamping(v.toInt());
174-
175163
/// Creates a [Int16] using two integers as high and low bits.
176164
///
177165
/// Each integer should be in the range of `0` to `2^(16 / 2) - 1`;
@@ -708,6 +696,7 @@ extension type const Int16._(int _) implements FixedInt {
708696
/// ```dart
709697
/// Int16(-3).abs(); // 3
710698
/// ```
699+
@redeclare
711700
Int16 abs() => Int16(_.abs());
712701

713702
/// Returns the absolute value of this integer.
@@ -774,6 +763,7 @@ extension type const Int16._(int _) implements FixedInt {
774763
/// Int16(1).bitLength(); // 1
775764
/// Int16(2).bitLength(); // 2
776765
/// ```
766+
@redeclare
777767
int get bitLength => _.bitLength;
778768

779769
/// Whether this integer is the minimum value representable by this type.
@@ -782,26 +772,6 @@ extension type const Int16._(int _) implements FixedInt {
782772
/// Whether this integer is the maximum value representable by this type.
783773
bool get isMax => identical(_, max);
784774

785-
/// Returns true if and only if this integer is even.
786-
///
787-
/// ## Example
788-
///
789-
/// ```dart
790-
/// Int16(2).isEven; // true
791-
/// Int16(3).isEven; // false
792-
/// ```
793-
bool get isEven => _.isEven;
794-
795-
/// Returns true if and only if this integer is odd.
796-
///
797-
/// ## Example
798-
///
799-
/// ```dart
800-
/// Int16(2).isOdd; // false
801-
/// Int16(3).isOdd; // true
802-
/// ```
803-
bool get isOdd => _.isOdd;
804-
805775
/// Returns the sign of this integer.
806776
///
807777
/// Returns 0 for zero, -1 for values less than zero and +1 for values greater
@@ -814,41 +784,23 @@ extension type const Int16._(int _) implements FixedInt {
814784
/// Int16(0).sign; // 0
815785
/// Int16(3).sign; // 1
816786
/// ```
787+
@redeclare
817788
Int16 get sign => Int16.fromUnchecked(_.sign);
818789

819-
/// Returns true if and only if this integer is zero.
820-
///
821-
/// ## Example
822-
///
823-
/// ```dart
824-
/// Int16(0).isZero; // true
825-
/// Int16(3).isZero; // false
826-
/// ```
827-
bool get isZero => _ == 0;
828-
829790
/// Returns true if and only if this integer is positive.
830791
///
831792
/// A positive integer is greater than zero.
832-
///
833-
/// ## Example
834-
///
835-
/// ```dart
836-
/// Int16(0).isPositive; // false
837-
/// Int16(3).isPositive; // true
838-
/// ```
839793
bool get isPositive => _ > 0;
840794

841-
/// Returns true if and only if this integer is negative.
842-
///
843-
/// A negative integer is less than zero.
795+
/// Returns true if and only if this integer is zero.
844796
///
845797
/// ## Example
846798
///
847799
/// ```dart
848-
/// Int16(-3).isNegative; // true
849-
/// Int16(0).isNegative; // false
800+
/// Int16(0).isZero; // true
801+
/// Int16(3).isZero; // false
850802
/// ```
851-
bool get isNegative => _ < 0;
803+
bool get isZero => _ == 0;
852804

853805
/// Returns `this` clamped to be in the range of [lowerLimit] and
854806
/// [upperLimit].
@@ -862,6 +814,7 @@ extension type const Int16._(int _) implements FixedInt {
862814
/// Int16(4).clamp(Int16(3), Int16(5)); // 4
863815
/// Int16(6).clamp(Int16(3), Int16(5)); // 5
864816
/// ```
817+
@redeclare
865818
Int16 clamp(Int16 lowerLimit, Int16 upperLimit) {
866819
return Int16.fromUnchecked(_.clamp(lowerLimit._, upperLimit._));
867820
}
@@ -871,24 +824,11 @@ extension type const Int16._(int _) implements FixedInt {
871824
/// The result r of this operation satisfies: `this == (this ~/ other) *`
872825
/// `other + r`. As a consequence, the remainder `r` has the same sign as the
873826
/// dividend `this`.
827+
@redeclare
874828
Int16 remainder(Int16 other) {
875829
return Int16.fromUnchecked(_.remainder(other._));
876830
}
877831

878-
/// This number as a [double].
879-
///
880-
/// If an integer number is not precisely representable as a [double], an
881-
/// approximation is returned.
882-
double toDouble() => _.toDouble();
883-
884-
/// This number as an [int].
885-
///
886-
/// This is the underlying integer representation of a [Int16], and is
887-
/// effectively an identity function, but for consistency and completeness,
888-
/// it is provided as a method to discourage casting.
889-
@redeclare
890-
int toInt() => _;
891-
892832
/// Returns this integer split into two parts: high and low bits.
893833
///
894834
/// The high bits are the most significant bits, and the low bits are the
@@ -901,6 +841,7 @@ extension type const Int16._(int _) implements FixedInt {
901841
/// The sign of the returned value is always positive.
902842
///
903843
/// See [num.operator %] for more details.
844+
@redeclare
904845
Int16 operator %(Int16 other) {
905846
return Int16.fromUnchecked(_ % other._);
906847
}
@@ -920,6 +861,7 @@ extension type const Int16._(int _) implements FixedInt {
920861
/// ```dart
921862
/// Int16(2) * Int16(3); // 6
922863
/// ```
864+
@redeclare
923865
Int16 operator *(Int16 other) => Int16(_ * other._);
924866

925867
/// Multiplies this number by other.
@@ -990,6 +932,7 @@ extension type const Int16._(int _) implements FixedInt {
990932
/// ```dart
991933
/// Int16(2) + Int16(3); // 5
992934
/// ```
935+
@redeclare
993936
Int16 operator +(Int16 other) => Int16(_ + other._);
994937

995938
/// Adds [other] to this number.
@@ -1060,6 +1003,7 @@ extension type const Int16._(int _) implements FixedInt {
10601003
/// ```dart
10611004
/// Int16(3) - Int16(2); // 1
10621005
/// ```
1006+
@redeclare
10631007
Int16 operator -(Int16 other) => Int16(_ - other._);
10641008

10651009
/// Subtracts [other] from this number.
@@ -1115,44 +1059,6 @@ extension type const Int16._(int _) implements FixedInt {
11151059
/// ```
11161060
Int16 clampedSubtract(Int16 other) => Int16.fromClamped(_ - other._);
11171061

1118-
/// Whether this number is numerically smaller than [other].
1119-
///
1120-
/// ## Example
1121-
///
1122-
/// ```dart
1123-
/// Int16(2) < Int16(3); // true
1124-
/// ```
1125-
bool operator <(Int16 other) => _ < other._;
1126-
1127-
/// Whether this number is numerically smaller than or equal to [other].
1128-
///
1129-
/// ## Example
1130-
///
1131-
/// ```dart
1132-
/// Int16(2) <= Int16(3); // true
1133-
/// Int16(3) <= Int16(3); // true
1134-
/// ```
1135-
bool operator <=(Int16 other) => _ <= other._;
1136-
1137-
/// Whether this number is numerically greater than [other].
1138-
///
1139-
/// ## Example
1140-
///
1141-
/// ```dart
1142-
/// Int16(3) > Int16(2); // true
1143-
/// ```
1144-
bool operator >(Int16 other) => _ > other._;
1145-
1146-
/// Whether this number is numerically greater than or equal to [other].
1147-
///
1148-
/// ## Example
1149-
///
1150-
/// ```dart
1151-
/// Int16(3) >= Int16(2); // true
1152-
/// Int16(3) >= Int16(3); // true
1153-
/// ```
1154-
bool operator >=(Int16 other) => _ >= other._;
1155-
11561062
/// The negation of `this`.
11571063
///
11581064
/// If the result is out of range, it asserts in debug mode, and wraps in
@@ -1168,6 +1074,7 @@ extension type const Int16._(int _) implements FixedInt {
11681074
/// ```dart
11691075
/// -Int16(3); // -3
11701076
/// ```
1077+
@redeclare
11711078
Int16 operator -() => Int16(-_);
11721079

11731080
/// The negation of `this`.
@@ -1234,18 +1141,21 @@ extension type const Int16._(int _) implements FixedInt {
12341141
/// ```dart
12351142
/// Int16(10) ~/ Int16(3); // 3
12361143
/// ```
1144+
@redeclare
12371145
Int16 operator ~/(Int16 other) => Int16.fromUnchecked(_ ~/ other._);
12381146

12391147
/// Bit-wise and operator.
12401148
///
12411149
/// See [int.operator &] for more details.
1150+
@redeclare
12421151
Int16 operator &(Int16 other) {
12431152
return _descriptor.uncheckedBinaryAnd(_, other._);
12441153
}
12451154

12461155
/// Bit-wise or operator.
12471156
///
12481157
/// See [int.operator |] for more details.
1158+
@redeclare
12491159
Int16 operator |(Int16 other) {
12501160
return _descriptor.uncheckedBinaryOr(_, other._);
12511161
}
@@ -1257,6 +1167,7 @@ extension type const Int16._(int _) implements FixedInt {
12571167
/// `pow(2, shiftAmount)`.
12581168
///
12591169
/// [shiftAmount] must be non-negative.
1170+
@redeclare
12601171
Int16 operator >>(int shiftAmount) {
12611172
return _descriptor.uncheckedShiftRight(_, shiftAmount);
12621173
}
@@ -1279,6 +1190,7 @@ extension type const Int16._(int _) implements FixedInt {
12791190
/// ```dart
12801191
/// Int16(3) << 2; // 12
12811192
/// ```
1193+
@redeclare
12821194
Int16 operator <<(int shiftAmount) {
12831195
return Int16(_descriptor.overflowingShiftLeft(_, shiftAmount));
12841196
}
@@ -1379,6 +1291,7 @@ extension type const Int16._(int _) implements FixedInt {
13791291
/// range.
13801292
/// - [clampedUnsignedShiftRight], which clamps the result if it is out of
13811293
/// range.
1294+
@redeclare
13821295
Int16 operator >>>(int shiftAmount) {
13831296
return Int16(_ >>> shiftAmount);
13841297
}
@@ -1457,6 +1370,7 @@ extension type const Int16._(int _) implements FixedInt {
14571370
/// Bit-wise exclusive-or operator.
14581371
///
14591372
/// See [int.operator ^] for more details.
1373+
@redeclare
14601374
Int16 operator ^(Int16 other) {
14611375
return _descriptor.uncheckedBinaryXor(_, other._);
14621376
}
@@ -1465,6 +1379,7 @@ extension type const Int16._(int _) implements FixedInt {
14651379
///
14661380
/// The bitwise compliment of an unsigned integer is its two's complement,
14671381
/// or the number inverted.
1382+
@redeclare
14681383
Int16 operator ~() {
14691384
return _descriptor.uncheckedBinaryNot(_);
14701385
}

0 commit comments

Comments
 (0)