Skip to content

Commit a6ddd24

Browse files
committed
v.embed_file: add .to_string() and .to_bytes() utility methods
1 parent e3c0f30 commit a6ddd24

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

doc/docs.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3558,12 +3558,10 @@ Full list of builtin options:
35583558
#### $embed_file
35593559

35603560
```v ignore
3561-
module main
3561+
import os
35623562
fn main() {
35633563
embedded_file := $embed_file('v.png')
3564-
mut fw := os.create('exported.png') or { panic(err.msg) }
3565-
fw.write_bytes(embedded_file.data(), embedded_file.len)
3566-
fw.close()
3564+
os.write_file('exported.png', embedded_file.to_string()) ?
35673565
}
35683566
```
35693567

vlib/v/embed_file/embed_file.v

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,22 @@ pub fn (mut ed EmbedFileData) free() {
3636
}
3737
}
3838

39+
pub fn (original &EmbedFileData) to_string() string {
40+
unsafe {
41+
mut ed := &EmbedFileData(original)
42+
the_copy := memdup(ed.data(), ed.len)
43+
return byteptr(the_copy).vstring_with_len(ed.len)
44+
}
45+
}
46+
47+
pub fn (original &EmbedFileData) to_bytes() []byte {
48+
unsafe {
49+
mut ed := &EmbedFileData(original)
50+
the_copy := memdup(ed.data(), ed.len)
51+
return the_copy.vbytes(ed.len)
52+
}
53+
}
54+
3955
pub fn (mut ed EmbedFileData) data() byteptr {
4056
if !isnil(ed.uncompressed) {
4157
return ed.uncompressed

0 commit comments

Comments
 (0)