~andrewrk/ziglang

8 4

[]u8{...} formatting escapes?

Details
Message ID
<DBAP190MB09825ADFDE2BF33A85C64FABF91DA@DBAP190MB0982.EURP190.PROD.OUTLOOK.COM>
DKIM signature
missing
Download raw message
Hi All, I am still busy on the PinePhone SoC in Zig.  I have a lot of 
const arrays of u8 bytes with pre-defined sequences that I need to send 
for things like initialising LCD.  And on a similar topic, a huge number 
of registers defined as packed struct(u32).

I am using vscodium under linux (to have access to Zig 0.10.1).  Its the 
first time I configured the ZLS, and I am not sure if my problem is 
related to that, or vscode, but whenever I have a nicely formated table 
of values, upon save the formatting is completely destroyed. So a dozen 
of the arrays now are single line and off the screen, and a bunch of 
shorter ones gets wrapped single value per line.

Similarly with the register structs, it would be nice to preserve the 
columnar formatting for ease of read and maintenance.

Is this vscodium, zls, or zig fmt behavior?  And most importantly, how 
can I configure it please?  ie any documenting escape sequences, or fmt 
directives available?

Cheers
Peter
Details
Message ID
<CV0MLQSHA5W4.2MG2JGZVENIHI@guix>
In-Reply-To
<DBAP190MB09825ADFDE2BF33A85C64FABF91DA@DBAP190MB0982.EURP190.PROD.OUTLOOK.COM> (view parent)
DKIM signature
missing
Download raw message
On 2023-08-24 at 09:47+02:00, Peter Bridge wrote:
> I have a lot of const arrays of u8 bytes with pre-defined sequences
> that I need to send for things like initialising LCD.

Not answering thy original question,
but thou might want to use @embedFile for microcodes:
https://ziglang.org/documentation/master#embedFile
Details
Message ID
<DBAP190MB09820F5F1F1E0579D1ACB5EBF91DA@DBAP190MB0982.EURP190.PROD.OUTLOOK.COM>
In-Reply-To
<CV0MLQSHA5W4.2MG2JGZVENIHI@guix> (view parent)
DKIM signature
missing
Download raw message
but good related information anyway thanks.  I used that trick for my 
WASM WebGL shader sources previously, since I am not keen the multi-line 
string prefix.

I am hoping I can use the same @embedFile trick for when I hopefully 
reach the ARM assembly boot code.  A long way to go still for that 
challenge :)

On 24/08/2023 10:18, Nguyễn Gia Phong wrote:
>
> Not answering thy original question,
> but thou might want to use @embedFile for microcodes:
> https://ziglang.org/documentation/master#embedFile
Details
Message ID
<CV0QFJHTY0UU.1YCPW2HUZ9ZHV@notebucik>
In-Reply-To
<DBAP190MB09825ADFDE2BF33A85C64FABF91DA@DBAP190MB0982.EURP190.PROD.OUTLOOK.COM> (view parent)
DKIM signature
missing
Download raw message
> So a dozen of the arrays now are single line and off the screen, and a
> bunch of shorter ones gets wrapped single value per line.

btw you can control this by choosing to include trailing comma or not.

In general any list of things in zig gets wrapped one item per line if
you include trailing comma or all items on one line if you don't.

Prokop

Array or Slice of [_]u8{...}

Details
Message ID
<DBAP190MB0982646C3675164DA5E4FB87F9E1A@DBAP190MB0982.EURP190.PROD.OUTLOOK.COM>
In-Reply-To
<CV0QFJHTY0UU.1YCPW2HUZ9ZHV@notebucik> (view parent)
DKIM signature
missing
Download raw message
Hi All, Now I have my nicely formated 'arrays of bytes' of various 
lengths, I wanted to combine groups of them into various sequences to 
iterate and send to the SoC.  I cant find the correct syntax:

```
const step1 = [_]u8{1,2,3,4};
const step2 = [_]u8{1,2,3,4,5,6,7,8};
const sequence1 = [_][]u8{
     step1,
     step2,
};
```

So trying to infer step count for some number of slices (and actually I 
have arrays), and so the compiler (0.10.1) helpfully tells me to use '&' 
on the sequence steps to coerce the arrays to []u8' but then it 
complains I now have a *const [4]u8'.

Is there a way to declare step1 as a slice in the first place? rather 
than an array?

```
const step1:[]u8 = [_]u8{1,2,3,4};
```
gives me the same tip about adding the '&' and then the same type error 
as above.

```
const step1:[]u8 = &[_]u8{1,2,3,4};
```
Anyway a slice is a pointer to an array that I would no longer have a 
reference to?? So even if it works it feels wrong...

Cheers Peter

Re: Array or Slice of [_]u8{...}

Details
Message ID
<CV3COERARBXR.K9XSEFNQOO0S@nix>
In-Reply-To
<DBAP190MB0982646C3675164DA5E4FB87F9E1A@DBAP190MB0982.EURP190.PROD.OUTLOOK.COM> (view parent)
DKIM signature
missing
Download raw message
On 2023-08-27 at 10:58+02:00, Peter Bridge wrote:
> const step1:[]u8 = &[_]u8{1,2,3,4};
> 
> Anyway a slice is a pointer to an array that I would no longer have a 
> reference to?? So even if it works it feels wrong...

I am not sure if there's a more concise way to write this,
but I'd go with something like

const step1 = [_]u8{1,2,3,4};
const step2 = [_]u8{1,2,3,4,5,6,7,8};
const sequence1 = [_][]u8{ step1[0..], step2[0..] };

Re: Array or Slice of [_]u8{...}

Details
Message ID
<DBAP190MB098207E5BA442FBAAF34DAE5F9E1A@DBAP190MB0982.EURP190.PROD.OUTLOOK.COM>
In-Reply-To
<CV3COERARBXR.K9XSEFNQOO0S@nix> (view parent)
DKIM signature
missing
Download raw message
Thanks for the reply but that seems to give the same type error as using 
'&'.  I take a fresh look in the morning, but think it might also be 
related to const'ness...

On 27/08/2023 15:10, Nguyễn Gia Phong wrote:
> I am not sure if there's a more concise way to write this,
> but I'd go with something like
>
> const step1 = [_]u8{1,2,3,4};
> const step2 = [_]u8{1,2,3,4,5,6,7,8};
> const sequence1 = [_][]u8{ step1[0..], step2[0..] };

Re: Array or Slice of [_]u8{...}

Details
Message ID
<5da0e303-4eb9-48d3-bd9a-bc9020c37018@app.fastmail.com>
In-Reply-To
<DBAP190MB098207E5BA442FBAAF34DAE5F9E1A@DBAP190MB0982.EURP190.PROD.OUTLOOK.COM> (view parent)
DKIM signature
missing
Download raw message
`step1` and `step2` are constants and cannot coerce to a slice of mutable bytes, `[]u8`. I think what you're looking for is `[]const u8`. The following will compile.

```
const step1 = [_]u8{1,2,3,4};
const step2 = [_]u8{1,2,3,4,5,6,7,8};
const sequence1 = [_][]const u8{
     &step1,
     &step2,
};
```

Re: Array or Slice of [_]u8{...}

Details
Message ID
<DBAP190MB0982BE5CAB6C9582AE329FDBF9E0A@DBAP190MB0982.EURP190.PROD.OUTLOOK.COM>
In-Reply-To
<5da0e303-4eb9-48d3-bd9a-bc9020c37018@app.fastmail.com> (view parent)
DKIM signature
missing
Download raw message
Thanks, yes it was the const.

So this works:

```
const step1: [] const u8  = &[_]u8{1,2,3,4};
const step2: [] const u8  = &[_]u8{1,2,3,4,5,6,7,8};
const sequence1 = [_][]const u8{
      step1,
      step2,
};

I think the whole confusion was the missing const on sequence. Since the 
compiler showed me two incompatible types:

error: expected type '[]u8', found '*const[4]u8'

but then goes on to mention discards const qualifier.  The following 
would have been clearer, but I guess that is not possible, as this is 
being coerced.

error: expected type '[]u8', found '[]const u8'


On 27/08/2023 23:20, Jay Petacat wrote:
> `step1` and `step2` are constants and cannot coerce to a slice of mutable bytes, `[]u8`. I think what you're looking for is `[]const u8`. The following will compile.
>
> ```
> const step1 = [_]u8{1,2,3,4};
> const step2 = [_]u8{1,2,3,4,5,6,7,8};
> const sequence1 = [_][]const u8{
>       &step1,
>       &step2,
> };
> ```
> .
>
Reply to thread Export thread (mbox)