Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add int/uint/float subtype export (int{8,16,32,64}, uint{8,16,32,64}, float{32,64}) and TypedObj #5

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

deepkolos
Copy link

add sub type export

  1. Int8
  2. Int16
  3. Int32
  4. Int64
  5. Uint8
  6. Uint16
  7. Uint32
  8. Uint64
  9. Float32
  10. Float64

and a TypedObj

image

image

Copy link
Member

@tsne tsne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @deepkolos,

Thanks for your contribution. I really like the idea of the proposed fixed-size types.
I added some comments on your code.

@@ -331,6 +346,31 @@ export function TypedMap<V>(keyT: Type<number|string>, valueT: Type<V>): Collect
};
}

export function TypedObj<T>(objT: Obj<Type<any>>): Collection<T> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should return a Type<Obj<any>> as Struct does. Therefore the type T can also be removed.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, before the commit fix: TypedObj's type error, but i use that in application code, typescript throw a type error

image

so i use in this way, to fix the error

image

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe this is not the right way to fix that problem

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please provide a minimal example where this error occurs? I'm not quite sure, if I understand your issue here.

src/types.ts Outdated
enc(buf: WriteBuffer, v: number): void {
buf.putUi8(tag);

switch(tag) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like that the switch is called for every enc call although the tag is fixed for the returned object. Maybe it is better to implement the fixed-size type (Int8, Int16, ...) and use them in Int.
You also miss the opportunity to encode a small integer with a "fixnum" tag (see msgpack spec).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for pointing out, i adjust that later

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done adjustment

src/types.ts Outdated
},

dec(buf: ReadBuffer): number {
return type.dec(buf);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please provide some more details how the suggested fixed-size types should behave?
For example, the Int8 type uses the same decoding function as the Int type. This can result in an value that does not fit in 8 bits. What is the advantage of the Int8 type if you can't be sure you always get an 8-bit integer?
Maybe you can elaborate a little bit more on this.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my situation is the 90Hz time value is Uint32, even overflowed is ok, but the length need to be Uint32, even value is small

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For your situation the overflow might be ok. But maybe we can develop a more general specification what the semantics of the fixed-size types should be. I see an advantage in the writing part. These types ensure, that you always write the specified tag and integer width into the binary stream. This could help in supporting existing protocols. Shouldn't it then be the same for the reading part?
So the question for me currently is: Should an IntX value should always be translated from and to the MessagePack "int X" wire format?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think
Int means min is Int8 max Int64
Int8 means min is Int16 max Int64
Int16 means min is Int16 max Int64
Int32 means min is Int32 max Int64

maybe should add suffix "unsafe" or better one ?
Int8Unsafe means force Int8
Int16Unsafe means force Int16
Int32Unsafe means force Int32

Copy link
Member

@tsne tsne Oct 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, then let's come to the following decission: An IntX type should always be encoded to and decoded from an X bit value ignoring overflow scenarios. This means that we always use and assume the binary format of int X. A UintX type should behave equivalently with unsigned values.
Does this meet your requirements?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

@deepkolos deepkolos requested a review from tsne October 19, 2020 02:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants