Skip to content
This repository has been archived by the owner on Feb 3, 2023. It is now read-only.

String roundup part 1 #1732

Open
wants to merge 28 commits into
base: develop
Choose a base branch
from
Open

Conversation

timotree3
Copy link
Collaborator

@timotree3 timotree3 commented Oct 3, 2019

PR summary

This PR is a follow-up to #1701 and is predicated upon it being merged. Review should start at commit 2788302.

This PR makes a few changes:

  • Replace &String -> &str in argument types and struct fields (2788302)
    • This gives more flexibility to the caller at no expense to the owner.
      See the commit message for more details.
  • Replace &str.clone() with &str.to_string() (7d043bc)
    • &str.clone() is useless and was left over from the functions that were taking &String.
  • Replace &String::from("...") -> "..." (a17a44a)
    • The conversion here is useless and was left over.
  • Rewrite &String::default() -> "" (e0aa8ec)
    • The allocation here is useless and was left over.
  • Remove some extraneous .to_string()s (eb018f0)
    • The allocation here is useless and was left over.
    • There are so many of these that I stopped in the middle of doing this.

Reviewers notes:

  • The "Cargo fmt" commits are just there to help separate out the actual changes.
    I can rebase and get rid of them after review.
  • I'm not sure if I changed anything public-facing, so I can't be sure which change log box to tick.

followups

In an ideal code-base, I would hope to see very few calls to .to_string(). (When does converting to a String add more information?)

The main situations in which I saw this used were for

  • Converting between String wrappers
    • (e.g. Address -> String -> JsonString, etc.)
  • Returning String-based errors
    • thingie().map_err(|_| "thingie failed!".to_string())?

I intend to open some follow up PRs and issues on these subjects to help minimize allocations and conversions.

changelog

Please check one of the following, relating to the CHANGELOG-UNRELEASED.md

  • this is a code change that effects some consumer (e.g. zome developers) of holochain core so it is added to the CHANGELOG-UNRELEASED.md (linked above), with the format - summary of change [PR#1234](https://github.com/holochain/holochain-rust/pull/1234)
  • this is not a code change, or doesn't effect anyone outside holochain core development

Taking &PathBufs is like taking &Strings, if you can't extend it because
it's not mutable, there's no use making sure it's on the heap. Taking
&Paths or &strs is simply more flexible to the caller without losing
anything for the procedure.

In the standard library, all code that wants to take an &Path takes
an impl AsRef<Path> instead, this way Strings and &strs can be passed
ergonomically. This commit uses &Path generally, because it may not be
worth the increase in binary size to add a generic. The author has no
preference one way or another and is open to rewriting to use the
alternative approach.
Resolves holochain#660

This change is valuable because you may obtain a path that
contains non-utf8 data and try to use it with these APIs,
but if these APIs take Strings, you have to lossily convert it
and you can't operate on those files.

The main non-obvious change as a result of this effort are changes like:

```
format!("using path: \'{}\'", some_string_path)
```
to
```
format!("using path: {:?}", some_pathbuf)
```

these result in a change from single quotes to double quotes.

The alternative substition is

```
format!("using path: \'{}\'", some_pathbuf.to_string_lossy())
```
this has the advantage of preserving single-quotes, but has the
disadvantage of requiring an extra allocation over the more
efficient Debug-impl of PathBuf.

There are existing instances in the code where the later approach is
used, it may be worth changing all instances to use the same choice.
I'm not sure how much of this was since last roundup and
how much I missed last time.
Note I only formatted the files I changed.
Did not a run full format in the name of keeping diff small.
@zippy
Copy link
Member

zippy commented Oct 21, 2019

Also here re the merge @timotree3. Thanks for the great work.

Since an `&String` can be converted into an `&str` at zero-cost,
the reverse does not apply, and
having an `&String` gives you access to no more APIs than an `&str`,
this is an indisputable improvment.
At this point the only remaining occurences of `&String`
are to do with either `JsonString` or `EntryType`.
In a later PR I intend to address these warts.
Holy cow, there are so many occurences!
I gave up after removing many.
@timotree3
Copy link
Collaborator Author

@zippy thanks :) I rebased and updated the description to point to the new commit hashes.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants