Flecs-Rust icon indicating copy to clipboard operation
Flecs-Rust copied to clipboard

use `&CStr` everywhere instead of `&str`

Open andrewgazelka opened this issue 1 year ago • 1 comments

In Rust 1.77.0, C-string literals have been stabilized and can be created with c"foo". As such, using CStrs are easy to create, and I believe we should aim to be as zero-cost as possible when it comes to our bindings.

  • Applying format_compact! to a str is a non-zero cost operation, even if the str is small.
  • My intuition tells be 90% of the time when using &'a str, 'a is 'static.

This would also allow us to unify the APIs that have two functions: one that takes a &str and one that takes a &CStr. There are many functions that do not even have a c_str equivalent, and this is personally an issue for me.

I also am slightly against the idea of using generics to accept CStr and str. Again, this will make newcomers more likely to use the much less efficient &str everywhere when it makes no sense to do so.

andrewgazelka avatar Jul 04 '24 01:07 andrewgazelka

This used to be &CStr, but from alpha user experience, I noticed it's not ideal and people are too unfamiliar with it. With compact string, the cost has been greatly reduced in most cases and thus it shouldn't be an issue in 99.9% of the cases considering the fact you aren't likely to use names in hot path code, but more likely setup code.

In the future we can consider some sort of Into trait to support both.

Indra-db avatar Jul 10 '24 12:07 Indra-db