Skip to content
This repository was archived by the owner on Oct 17, 2024. It is now read-only.

Commit 996e376

Browse files
committed
Added IdentityCodec
1 parent 62fc078 commit 996e376

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

lib/convert.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ library convert;
77
export 'src/accumulator_sink.dart';
88
export 'src/byte_accumulator_sink.dart';
99
export 'src/hex.dart';
10+
export 'src/identity_codec.dart';
1011
export 'src/percent.dart';
1112
export 'src/string_accumulator_sink.dart';

lib/src/identity_codec.dart

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import 'dart:convert';
2+
3+
class _IdentityConverter<T> extends Converter<T, T> {
4+
@override
5+
T convert(T input) => input;
6+
}
7+
8+
/// A [Codec] that converts from T to T doing absolutely nothing.
9+
///
10+
/// This is the identity codec, the one that passes input to output when
11+
/// decoding and encoding. This codec is mostly useful when implementing generic
12+
/// code that can be fused with a [Codec] and you need an identity codec for the
13+
/// base case.
14+
///
15+
/// Note, that when fused with another [Codec] the identity codec disppears.
16+
class IdentityCodec<T> extends Codec<T, T> {
17+
@override
18+
Converter<T, T> get decoder => _IdentityConverter();
19+
@override
20+
Converter<T, T> get encoder => _IdentityConverter();
21+
22+
/// Fuse with an other codec.
23+
///
24+
/// This will return the [other] codec, as the identity codec is trivial.
25+
@override
26+
Codec<T, R> fuse<R>(Codec<T, R> other) => other;
27+
}

0 commit comments

Comments
 (0)