Skip to content

Commit

Permalink
Merge branch '6-nullable-t-for-optional-fields-are-call' into 'master'
Browse files Browse the repository at this point in the history
Resolve "`Nullable<T>` for optional fields are call"

Closes kaitai-io#6

See merge request marta/kaitai_struct_csharp_runtime!14
  • Loading branch information
pluskal committed Jul 3, 2020
2 parents c071b83 + 7c10982 commit a738aff
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions Kaitai.Struct.Runtime.Async/KaitaiAsyncStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public KaitaiAsyncStream(byte[] bytes) : this(new MemoryStream(bytes))
public ValueTask<long> GetSizeAsync() => _readerContext.GetSizeAsync();

public virtual async Task SeekAsync(long position) => await _readerContext.SeekAsync(position);
public virtual async Task SeekAsync(ulong position) => await SeekAsync((long)position);

public override long Pos => _readerContext.Position;

Expand Down
26 changes: 26 additions & 0 deletions Kaitai.Struct.Runtime/ImplicitNullable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Kaitai
{
public struct ImplicitNullable<T> where T : struct
{
public bool HasValue { get { return this._value.HasValue; } }
public T Value { get { return this._value.Value; } }

public ImplicitNullable(T value) : this() { this._value = value; }
public ImplicitNullable(T? value) : this() { this._value = value; }

public static implicit operator ImplicitNullable<T>(T value) { return new ImplicitNullable<T>(value); }
public static implicit operator ImplicitNullable<T>(T? value) { return new ImplicitNullable<T>(value); }

public static implicit operator T(ImplicitNullable<T> value) { return value._value ?? default(T); }
public static implicit operator T?(ImplicitNullable<T> value) { return value._value; }

private T? _value { get; set; }

// Should define other Nullable<T> members, especially
// Equals and GetHashCode to avoid boxing
}
}
1 change: 1 addition & 0 deletions Kaitai.Struct.Runtime/KaitaiStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ protected BinaryReader BinaryReader
public override bool IsEof => BaseStream.Position >= BaseStream.Length && BitsLeft == 0;

public void Seek(long position) => BaseStream.Seek(position, SeekOrigin.Begin);
public void Seek(ulong position) => Seek((long)position);

public override long Pos => BaseStream.Position;

Expand Down

0 comments on commit a738aff

Please sign in to comment.