Skip to content

Help with guaranteeing CoW going from Erlang to Rust #649

Answered by hansihe
josevalim asked this question in Q&A

You must be logged in to vote

The first question is: does this allocate a new string or it simply points to the binary?

This indeed allocates a new string, as the rust String type manages its own memory.

However, in case it does make a copy, how could we go from Term to Cow?

If we decode the term to a &str, that does not allocate and instead points to the binary allocated by the BEAM.

We can then use the &str to construct a Cow, which should not allocate.

The slightly tricky part comes from managing the lifetimes, we need to introduce an explicit lifetime annotation to carry the lifetime of the input term into the output type.

fn term_to_encode_input<'a, 'b>(term: &'a Term<'b>) -> Result<EncodeInput<'b>, ExTokeniz…

Replies: 2 comments 6 replies

You must be logged in to vote
1 reply
@josevalim

Answer selected by josevalim

You must be logged in to vote
5 replies
@josevalim

@josevalim

@hansihe

@josevalim

@josevalim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants