RillDecoder<A>
A streaming binary decoder that drives a Decoder over a Rill of BitVectors, emitting decoded values as they become available.
RillDecoder is built by composing static factories and combinators before being run via toPipe, toPipeByte, or decode. Each factory controls how many stream elements are consumed and what happens on error.
final decoder = RillDecoder.many(Codecs.int32);
final ints = Rill.emits(bytes).through(decoder.toPipeByte);Properties
hashCode no setter inherited
The hash code for this object.
A hash code is a single integer which represents the state of the object that affects operator == comparisons.
All objects have hash codes. The default hash code implemented by Object represents only the identity of the object, the same way as the default operator == implementation only considers objects equal if they are identical (see identityHashCode).
If operator == is overridden to use the object state instead, the hash code must also be changed to represent that state, otherwise the object cannot be used in hash based data structures like the default Set and Map implementations.
Hash codes must be the same for objects that are equal to each other according to operator ==. The hash code of an object should only change if the object changes in a way that affects equality. There are no further requirements for the hash codes. They need not be consistent between executions of the same program and there are no distribution guarantees.
Objects that are not equal are allowed to have the same hash code. It is even technically allowed that all instances have the same hash code, but if clashes happen too often, it may reduce the efficiency of hash-based data structures like HashSet or HashMap.
If a subclass overrides hashCode, it should override the operator == operator as well to maintain consistency.
Inherited from Object.
Implementation
external int get hashCode;runtimeType no setter inherited
A representation of the runtime type of the object.
Inherited from Object.
Implementation
external Type get runtimeType;toPipe no setter
Exposes this decoder as a Pipe from BitVector chunks to decoded values.
Implementation
Pipe<BitVector, A> get toPipe => (rill) => decode(rill);toPipeByte no setter
Exposes this decoder as a Pipe from raw bytes to decoded values.
Implementation
Pipe<int, A> get toPipeByte =>
(rill) => rill.chunks().map((chunk) => chunk.toBitVector).through(toPipe);Methods
append()
Sequences this decoder followed by s2 on the remaining input.
Implementation
RillDecoder<A> append(Function0<RillDecoder<A>> s2) => RillDecoder._(Append(this, s2));call()
Low-level pull-based entry point; runs the decoder and returns any unconsumed remainder of the input stream.
Implementation
Pull<A, Option<Rill<BitVector>>> call(Rill<BitVector> r) {
switch (_step) {
case Empty _:
return Pull.pure(Some(r));
case Result(:final value):
return Pull.output1(value).as(Some(r));
case Failed(:final reason, :final stackTrace):
return Pull.raiseError(reason, stackTrace);
case Append(:final x, :final y):
return x(r).flatMap(
(next) => next.fold(
() => Pull.pure(const None()),
(rem) => y()(rem),
),
);
case Decode(f: final decoder, :final once, :final failOnErr):
Pull<A, Option<Rill<BitVector>>> loop(
BitVector carry,
Rill<BitVector> r,
Option<Err> carriedError,
) {
return r.pull.uncons1.flatMap((hdtl) {
return hdtl.foldN(
() {
late final Pull<A, Option<Rill<BitVector>>> done =
carry.isEmpty ? Pull.pure(none()) : Pull.pure(Some(Rill.emit(carry)));
return carriedError.filter((_) => failOnErr).fold(
() => done,
(err) {
if (!once && err is InsufficientBits) {
return done;
} else {
return Pull.raiseError('Codec Error: $err');
}
},
);
},
(hd, tl) {
final buffer = carry.concat(hd);
return decoder(buffer).fold(
(err) {
if (err is InsufficientBits) {
return loop(buffer, tl, Some(err));
} else if (failOnErr) {
return Pull.raiseError('Codec Error: $err');
} else {
return Pull.pure(Some(tl.cons1(buffer)));
}
},
(success) {
final next = success.remainder.isEmpty ? tl : tl.cons1(success.remainder);
final p = success.value(next);
if (once) {
return p;
} else {
return p.flatMap((nextOpt) {
return nextOpt.fold(
() => Pull.pure(none()),
(next) => loop(BitVector.empty, next, carriedError),
);
});
}
},
);
},
);
});
}
return loop(BitVector.empty, r, none());
case Isolate(:final bits, :final decoder):
Pull<A, Option<Rill<BitVector>>> loop(
BitVector carry,
Rill<BitVector> r,
Option<Err> carriedError,
) {
return r.pull.uncons1.flatMap((hdtl) {
return hdtl.foldN(
() => carriedError.fold(
() => carry.isEmpty ? Pull.pure(none()) : Pull.pure(Some(Rill.emit(carry))),
(e) => Pull.raiseError('Codec Error: $e'),
),
(hd, tl) {
final (buffer, remainder) = carry.concat(hd).splitAt(bits);
if (buffer.size == bits) {
return decoder(
Rill.emit(buffer),
).append(() => Pull.pure(Some(tl.cons1(remainder))));
} else {
return loop(buffer, tl, Some(Err.insufficientBits(bits, buffer.size)));
}
},
);
});
}
return loop(BitVector.empty, r, none());
}
}decode()
Runs this decoder against rill, returning a stream of decoded values.
Implementation
Rill<A> decode(Rill<BitVector> rill) => this(rill).voided.rillNoScope;filter()
Discards decoded values that do not satisfy p.
Implementation
RillDecoder<A> filter(Function1<A, bool> p) =>
flatMap((a) => p(a) ? RillDecoder.emit(a) : RillDecoder.empty);flatMap()
Chains decoders: after each emitted value a, runs f to produce the next decoder, which continues from the remaining input.
Implementation
RillDecoder<B> flatMap<B>(Function1<A, RillDecoder<B>> f) {
return RillDecoder._(switch (_step) {
Empty _ => Empty(),
Result(:final value) => f(value)._step,
Failed(:final reason, :final stackTrace) => Failed(reason, stackTrace),
Decode(f: final g, :final once, :final failOnErr) => Decode(
(bv) => g(bv).map((res) => res.map((a) => a.flatMap(f))),
once,
failOnErr,
),
Isolate(:final bits, :final decoder) => Isolate(bits, decoder.flatMap(f)),
Append(:final x, :final y) => Append(x.flatMap(f), () => y().flatMap(f)),
});
}handleErrorWith()
Recovers from a decode error by running f to produce a fallback decoder.
Implementation
RillDecoder<A> handleErrorWith(Function1<Object, RillDecoder<A>> f) {
return RillDecoder._(switch (_step) {
Empty _ => Empty(),
Result(:final value) => Result(value),
Failed(:final reason) => f(reason)._step,
Decode(f: final g, :final once, :final failOnErr) => Decode(
(bv) => g(bv).map((res) => res.map((a) => a.handleErrorWith(f))),
once,
failOnErr,
),
Isolate(:final bits, :final decoder) => Isolate(bits, decoder.handleErrorWith(f)),
Append(:final x, :final y) => Append(x.handleErrorWith(f), () => y().handleErrorWith(f)),
});
}map()
Transforms each decoded value with f.
Implementation
RillDecoder<B> map<B>(Function1<A, B> f) => flatMap((a) => RillDecoder.emit(f(a)));noSuchMethod() inherited
Invoked when a nonexistent method or property is accessed.
A dynamic member invocation can attempt to call a member which doesn't exist on the receiving object. Example:
dynamic object = 1;
object.add(42); // Statically allowed, run-time errorThis invalid code will invoke the noSuchMethod method of the integer 1 with an Invocation representing the .add(42) call and arguments (which then throws).
Classes can override noSuchMethod to provide custom behavior for such invalid dynamic invocations.
A class with a non-default noSuchMethod invocation can also omit implementations for members of its interface. Example:
class MockList<T> implements List<T> {
noSuchMethod(Invocation invocation) {
log(invocation);
super.noSuchMethod(invocation); // Will throw.
}
}
void main() {
MockList().add(42);
}This code has no compile-time warnings or errors even though the MockList class has no concrete implementation of any of the List interface methods. Calls to List methods are forwarded to noSuchMethod, so this code will log an invocation similar to Invocation.method(#add, [42]) and then throw.
If a value is returned from noSuchMethod, it becomes the result of the original invocation. If the value is not of a type that can be returned by the original invocation, a type error occurs at the invocation.
The default behavior is to throw a NoSuchMethodError.
Inherited from Object.
Implementation
@pragma("vm:entry-point")
@pragma("wasm:entry-point")
external dynamic noSuchMethod(Invocation invocation);toString() inherited
A string representation of this object.
Some classes have a default textual representation, often paired with a static parse function (like int.parse). These classes will provide the textual representation as their string representation.
Other classes have no meaningful textual representation that a program will care about. Such classes will typically override toString to provide useful information when inspecting the object, mainly for debugging or logging.
Inherited from Object.
Implementation
external String toString();Operators
operator ==() inherited
The equality operator.
The default behavior for all Objects is to return true if and only if this object and other are the same object.
Override this method to specify a different equality relation on a class. The overriding method must still be an equivalence relation. That is, it must be:
Total: It must return a boolean for all arguments. It should never throw.
Reflexive: For all objects
o,o == omust be true.Symmetric: For all objects
o1ando2,o1 == o2ando2 == o1must either both be true, or both be false.Transitive: For all objects
o1,o2, ando3, ifo1 == o2ando2 == o3are true, theno1 == o3must be true.
The method should also be consistent over time, so whether two objects are equal should only change if at least one of the objects was modified.
If a subclass overrides the equality operator, it should override the hashCode method as well to maintain consistency.
Inherited from Object.
Implementation
external bool operator ==(Object other);Static Properties
empty final
A decoder that emits nothing and signals end-of-stream.
Implementation
static final RillDecoder<Never> empty = RillDecoder._(Empty());Static Methods
emit()
Creates a decoder that emits a immediately without consuming any input.
Implementation
static RillDecoder<A> emit<A>(A a) => RillDecoder._(Result(a));emits()
Creates a decoder that emits all values without consuming any input.
Implementation
static RillDecoder<A> emits<A>(List<A> values) =>
values.fold(RillDecoder<A>._(Empty()), (acc, a) => acc.append(() => RillDecoder.emit(a)));ignore()
Skips bits bits of input without emitting any value.
Implementation
static RillDecoder<Never> ignore<A>(int bits) =>
once(Codec.ignore(bits)).flatMap((_) => RillDecoder.empty);isolate()
Runs decoder against exactly bits bits from the stream, then passes the remaining input to the next decoder.
Implementation
static RillDecoder<A> isolate<A>(int bits, RillDecoder<A> decoder) =>
RillDecoder._(Isolate(max(0, bits), decoder));many()
Repeatedly applies decoder to the stream, emitting each decoded value.
Raises an error if decoder fails with anything other than insufficient bits at end-of-stream.
Implementation
static RillDecoder<A> many<A>(Decoder<A> decoder) => RillDecoder._(
Decode(
(bv) => decoder.decode(bv).map((res) => res.map((a) => RillDecoder.emit(a))),
false,
true,
),
);once()
Applies decoder exactly once, consuming bits until a value is decoded.
Raises an error on decode failure or if the stream ends before enough bits are available.
Implementation
static RillDecoder<A> once<A>(Decoder<A> decoder) => RillDecoder._(
Decode(
(bv) => decoder.decode(bv).map((res) => res.map((a) => RillDecoder.emit(a))),
true,
true,
),
);raiseError()
Creates a decoder that immediately raises err.
Implementation
static RillDecoder<Never> raiseError(Object err) => RillDecoder._(Failed(err));tryMany()
Like many but stops quietly when decoder fails instead of raising an error.
Implementation
static RillDecoder<A> tryMany<A>(Decoder<A> decoder) => RillDecoder._(
Decode(
(bv) => decoder.decode(bv).map((res) => res.map((a) => RillDecoder.emit(a))),
false,
false,
),
);tryOnce()
Like once but leaves the stream untouched when decoder fails instead of raising an error.
Implementation
static RillDecoder<A> tryOnce<A>(Decoder<A> decoder) => RillDecoder._(
Decode(
(bv) => decoder.decode(bv).map((res) => res.map((a) => RillDecoder.emit(a))),
true,
false,
),
);