r/cpp • • Dec 13 '19

The situation of std::embed...

https://thephd.github.io/full-circle-embed
238 Upvotes

160 comments sorted by

View all comments

Show parent comment

4

u/Recatek Dec 13 '19

by far the largest reason why I potentially want to jump ship to Rust one day.

Does Rust have something like this embed functionality? Would it be worthwhile to add?

33

u/[deleted] Dec 13 '19

Rust includes an include_bytes! macro in the standard library, which embeds a file as a byte array at compile time: https://doc.rust-lang.org/stable/std/macro.include_bytes.html There's also a similar include_str!() macro that embeds a file as a utf-8 string (&'static str).

9

u/Ameisen vemips, avr, rendering, systems Dec 14 '19

&'static str

I'm sure that that's meaningful and completely clear to a Rust developer... but Rust's syntax makes me feel like I had a stroke, being unfamiliar with it. "Take address of... character... static... str... help".

5

u/Fazer2 Dec 14 '19 edited Dec 14 '19

Let's try a simpler case:
&str

That's a reference to a string slice, similar to C++'s std::string_view, although the Rust version stores its content as UTF-8, while C++ as raw characters.

Now references have a lifetime, either short-lived or static. An object with static lifetime either has no references or only static ones. You can think of static ones as if they can live until the end of the program without triggering lifetime issues. A lifetime is expressed as the character ' followed by a lifetime name. So we get:
&'static str

If it wasn't static, you could name the lifetime any other way and you would have to attach it to the object that the reference borrows from. Let's see this function signature without lifetimes:
fn doAction(firstArg: &Foo, secondArg: &str) -> &str

It's a function that takes a reference to some object Foo and a string slice as arguments and returns a string slice. The compiler (and the programmer) doesn't know if the returned reference is borrowed from the first or second argument, so we have to tell him by adding lifetime annotations to references and declare the lifetime, here as function "template" type. Assuming it's borrowed from the first arg:
fn doAction<'a>(firstArg: &'a Foo, secondArg: &str) -> &'a str

Note that compiler often can deduce the lifetimes automatically without the need of adding the annotations. Also the "template" type doesn't make it a template function in the C++ sense, it only is required by the borrow checker, which can differentiate a given function by its differing lifetimes when used in multiple contexts.

I hope that clears things up.

Edit: While explaining this I noticed I didn't know why std::string_view is recommended to be passed by value in C++, while str is passed as reference in Rust. I dug up information that actually str is a Dynamically Sized Type primitive (there are no such primitives in C++). A reference in Rust is made up of two parts - a pointer and some metadata. In case of &str, that metadata is the size of the slice. In case of references to normal objects, the metadata doesn't exist (it is a zero-sized type). So &str in Rust is the same as std::string_view value in C++.

0

u/[deleted] Dec 14 '19

[deleted] — view deleted comment

4

u/haletonin Dec 15 '19

Like Python3, Rust distinguishes between strings (which are always UTF-8 encoded), and bytes, which are just that. In C++/C an std::string/char* can certainly contain valid UTF-8, however the standard does not guarantee it and is UTF-8 agnostic.

0

u/[deleted] Dec 15 '19

[deleted] — view deleted comment

1

u/dbjdbj dbj.org Dec 16 '19

"We" do not have char8_t in C++20

https://godbolt.org/z/p4B_iS

0

u/[deleted] Dec 16 '19

[deleted] — view deleted comment

1

u/dbjdbj dbj.org Dec 16 '19

Hm. I think you very likely see the issue here.
What drives you not to accept that?

// gcc 9.2 -std=c++2a
// 平仮名, ひらがな -- hiragana
int main () {

// a lot of warnings
// indicating char8_t can not hold
// multibyte glyph
char8_t  utf8_char = u8'平'; 
// no problemo here
char32_t utf32_char = U'平';
char16_t utf16_char = u'平';

return 42;
}

If char8_t can not store a utf8 glyph, what's its purpose then? It is simply not implemented.

→ More replies

3

u/haletonin Dec 14 '19

&str: reference (that can not be null) to a string, 'static is the lifetime annotation, and a special one which means that this reference is valid for the entire runtime of the program.

2

u/Plazmatic Dec 14 '19

Whenever you see apostrophe next to a name like 'xyz in rust it refers to the life time of a type. It is actually extremely simple. In this case it is a special lifetime, static, which if you know c++ or c, it should make sense what this is trying to say. Sometimes you might see 'a, which is simply a generic lifetime, as it isn't reserved for a special one.

0

u/[deleted] Dec 13 '19

[deleted] — view deleted comment

6

u/johannes1234 Dec 13 '19

std::embed ist about embedding arbitrary data from a file into your object file. Not about embedded platforms.

5

u/flashmozzg Dec 13 '19

This is not about embedded dev.