This repository has been archived by the owner on Feb 3, 2023. It is now read-only.
String roundup part 1 #1732
Open
timotree3
wants to merge
28
commits into
holochain:develop
Choose a base branch
from
timotree3:string-roundup2
base: develop
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
String roundup part 1 #1732
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
11 tasks
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
force-pushed
the
string-roundup2
branch
from
October 21, 2019 16:25
f4a8839
to
eae08db
Compare
@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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:
&String
->&str
in argument types and struct fields (2788302)See the commit message for more details.
&str.clone()
with&str.to_string()
(7d043bc)&str.clone()
is useless and was left over from the functions that were taking&String
.&String::from("...")
->"..."
(a17a44a)&String::default()
->""
(e0aa8ec).to_string()
s (eb018f0)Reviewers notes:
I can rebase and get rid of them after review.
followups
In an ideal code-base, I would hope to see very few calls to
.to_string()
. (When does converting to aString
add more information?)The main situations in which I saw this used were for
String
wrappersAddress
->String
->JsonString
, etc.)String
-based errorsthingie().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
- summary of change [PR#1234](https://github.com/holochain/holochain-rust/pull/1234)