data: add Nothing type

This commit is contained in:
Shiz 2021-06-26 05:29:10 +02:00
parent dda5b1aa87
commit 3741e353e3
2 changed files with 27 additions and 4 deletions

View File

@ -4,7 +4,7 @@ from .core.io import Stream, Segment
from .core.meta import Wrapper, Generic
from .core.expr import Expr
from .types.data import Data, data
from .types.data import Nothing, Data, data
from .types.num import *
from .types.struct import StructType, Struct
from .types.seq import Arr, Tuple
@ -16,7 +16,7 @@ __all__ = [x.__name__ for x in {
Context, Type, Stream, Segment, Expr,
Wrapper, Default, Sized, Ref, Transform, Mapped, Enum, Switch,
AlignTo, AlignedTo,
Data,
Nothing, Data,
Int, Bool, Float,
Arr, Tuple,
StructType, Struct, Generic,

View File

@ -1,6 +1,29 @@
from typing import Optional as O, Union as U
from ..core.base import Type, Context, PossibleDynamic as D
from ..core.io import Stream
from ..core.io import Stream, Pos
class Nothing(Type[None]):
def __init__(self) -> None:
pass
def parse(self, context: Context, stream: Stream) -> None:
return None
def dump(self, context: Context, stream: Stream, value: None) -> None:
pass
def sizeof(self, context: Context, value: None) -> O[Pos]:
return 0
def default(self, context: Context) -> None:
return None
def __str__(self) -> str:
return 'Nothing'
def __repr__(self) -> str:
return '{__name__}.Nothing()'
class Data(Type[bytes]):
__slots__ = ('size',)
@ -23,7 +46,7 @@ class Data(Type[bytes]):
size = 0
return bytes(size)
def sizeof(self, context: Context, value: O[bytes]) -> O[int]:
def sizeof(self, context: Context, value: O[bytes]) -> O[Pos]:
return context.peek(self.size)
data = Data()