-
-
Notifications
You must be signed in to change notification settings - Fork 77
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor: modify Vec<&str> to Vec<String> (#73) #75
Conversation
fix issue#73 Use Vec<String> instead of Vec<&str> to avoid lifetime issue
Codecov Report
@@ Coverage Diff @@
## master #75 +/- ##
==========================================
+ Coverage 80.46% 80.58% +0.11%
==========================================
Files 18 18
Lines 2580 2678 +98
==========================================
+ Hits 2076 2158 +82
- Misses 504 520 +16
Continue to review full report at Codecov.
|
@hackerchai We don't need to change Only change the returned |
Hello @xcaptain, here I found a good post which explains why String is better than &str in some situations. rust-lang/rust#46966 I think using &str in casbin-rs is not realistic, although it may be more efficient. Image that a client generated a Vec as policies, and he wants to use in casbin-rs, he has to converted Vec to Vec<&str>. That's not convenient. In realworld example, we always get |
An example of code is the following which I encountered in my personal project: fn load_records<P: AsRef<Path>>(p: P) -> Vec<StringRecord> {
if let Ok(mut rdr) = ReaderBuilder::new()
.has_headers(false)
.double_quote(false)
.comment(Some(b'#'))
.delimiter(b',')
.trim(Trim::All)
.from_path(p)
{
if let Ok(records) = rdr.records().collect::<Result<Vec<StringRecord>, Error>>() {
return records
.into_iter()
.filter(|r| is_valid_policy(r) || is_valid_grouping_policy(r))
.collect::<Vec<StringRecord>>();
}
}
vec![]
} In the above code I was trying to read a Then if I tried to do so, I'll got lifetime issue because the |
Ok, I agree to use owned string in all the public APIs. |
Thanks @xcaptain and glad to see you agree with me! |
Found a RFC here https://github.com/rust-lang/rfcs/blob/master/text/0241-deref-conversions.md After it get implemented we don't need to worry whether use String or &str anymore, the compiler can help us. |
I'm pretty sure auto-deref is implemented I think what it allows is fn use_vector<T>(vec: &[T]) {}
fn use_string(s: &str) {}
let v = vec![0, 1, 2];
let s = String::new();
vector(&v);
use_string(&s); |
@DevinR528 You are right, my mistake, RFC 241 has been merged. I was think to write a function that can take How about we use |
@xcaptain @DevinR528 I think It's a common pattern to use It's hard to find a situation in which we generated |
@GopherJ as you know (you also tried to get the Api traits to take an pub struct Enforcer<A: Adaptor> {
adaptor: A where it only accepts one instance. |
@DevinR528 Yes I agree, I removed static dispatcher because it causes some constraints.
Anyway I think we should stick to |
I think we can merge this. Thanks @hackerchai for your contributions! |
fix issue#73
Use Vec instead of Vec<&str> to avoid lifetime issue