Dataless union field

9 views
Skip to first unread message

Igor Bukanov

unread,
Jul 29, 2021, 2:33:52 PM7/29/21
to chromium-mojo
Hello,

Suppose I need a union to represent a result of a read operation that can either return a buffer, an error with the message or end-of-file status. I suppose I can use something like:

union ReadResult {
  DataBuffer data;
  string error_message;
  bool end_of_file;
};

But this is not ideal since it allows to construct a union with a meaningless value where end_of_file is set but it is set to false. Ideally I would like to have something like:

union ReadResult {
  DataBuffer data;
  string error_message;
  void end_of_file;
};

where the void indicates that a particular branch of the union has no data and exists solely to get a new unique union tag. Is something like that possible?

Regards, Igor


Ken Rockot

unread,
Jul 29, 2021, 2:38:33 PM7/29/21
to Igor Bukanov, chromium-mojo
Not currently possible. This is bug 1220106.

--
You received this message because you are subscribed to the Google Groups "chromium-mojo" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-moj...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-mojo/3b2cc272-5dce-4bc9-a988-69ec8349d9fdn%40chromium.org.

Daniel Cheng

unread,
Jul 29, 2021, 2:48:26 PM7/29/21
to Ken Rockot, Igor Bukanov, chromium-mojo
For now, just use uint8 or bool or your favorite small type of choice and add a TODO() pointing to the bug rockot@ linked.

Daniel

Igor Bukanov

unread,
Jul 29, 2021, 3:57:05 PM7/29/21
to Daniel Cheng, Ken Rockot, chromium-mojo
Hm, I realized I can use a enum with a single value like

enum Unit {
kUnit
};

that gives

union ReadResult {
DataBuffer data;
string error_message;
Unit end_of_file;
};

This compiles and it seems to work. But does it have the same efficiency
as of using bool end_of_file?

On 2021-07-29 20:47, Daniel Cheng wrote:
> For now, just use uint8 or bool or your favorite small type of choice
> and add a TODO() pointing to the bug rockot@ linked.
>
> Daniel
>
> On Thu, 29 Jul 2021 at 11:38, 'Ken Rockot' via chromium-mojo
> <chromi...@chromium.org> wrote:
>
>> Not currently possible. This is bug 1220106 [2].
>>> [1].
>>
>> --
>> You received this message because you are subscribed to the Google
>> Groups "chromium-mojo" group.
>> To unsubscribe from this group and stop receiving emails from it,
>> send an email to chromium-moj...@chromium.org.
>> To view this discussion on the web visit
>>
> https://groups.google.com/a/chromium.org/d/msgid/chromium-mojo/CA%2BapAgFmY%2Bf96KGvm%3DgSEUsQ%2B0xGjQyrY8Doog8bqZYy5gDUJw%40mail.gmail.com
>> [3].
>
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "chromium-mojo" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/a/chromium.org/d/topic/chromium-mojo/SCCdY5j5u8Q/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> chromium-moj...@chromium.org.
> To view this discussion on the web visit
> https://groups.google.com/a/chromium.org/d/msgid/chromium-mojo/CAF3XrKptnPYdiAjuDpTvVXETjkFRnhK0DZzFhKN%3DtZuiXC8jLA%40mail.gmail.com
> [4].
>
>
> Links:
> ------
> [1]
> https://groups.google.com/a/chromium.org/d/msgid/chromium-mojo/3b2cc272-5dce-4bc9-a988-69ec8349d9fdn%40chromium.org?utm_medium=email&amp;utm_source=footer
> [2] https://crbug.com/1220106
> [3]
> https://groups.google.com/a/chromium.org/d/msgid/chromium-mojo/CA%2BapAgFmY%2Bf96KGvm%3DgSEUsQ%2B0xGjQyrY8Doog8bqZYy5gDUJw%40mail.gmail.com?utm_medium=email&amp;utm_source=footer
> [4]
> https://groups.google.com/a/chromium.org/d/msgid/chromium-mojo/CAF3XrKptnPYdiAjuDpTvVXETjkFRnhK0DZzFhKN%3DtZuiXC8jLA%40mail.gmail.com?utm_medium=email&utm_source=footer

Ken Rockot

unread,
Jul 29, 2021, 4:02:39 PM7/29/21
to Igor Bukanov, Daniel Cheng, chromium-mojo
There's no difference in efficiency between using an enum, a bool, or a uint8/16/32/64 in this case. Within a serialized union they all cost the same.
Reply all
Reply to author
Forward
0 new messages