Skip to content
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

Merged
merged 2 commits into from
Apr 4, 2020
Merged

Refactor: modify Vec<&str> to Vec<String> (#73) #75

merged 2 commits into from
Apr 4, 2020

Conversation

hackerchai
Copy link
Member

fix issue#73

Use Vec instead of Vec<&str> to avoid lifetime issue

fix issue#73

Use Vec<String> instead of Vec<&str> to avoid lifetime issue
@codecov
Copy link

codecov bot commented Apr 1, 2020

Codecov Report

Merging #75 into master will increase coverage by 0.11%.
The diff coverage is 85.21%.

Impacted file tree graph

@@            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     
Impacted Files Coverage Δ
src/internal_api.rs 42.05% <ø> (ø)
src/rbac_api.rs 83.98% <71.62%> (-2.24%) ⬇️
src/management_api.rs 78.77% <88.74%> (+3.07%) ⬆️
src/adapter/file_adapter.rs 47.91% <100.00%> (ø)
src/adapter/memory_adapter.rs 35.06% <100.00%> (ø)
src/enforcer.rs 84.01% <100.00%> (ø)
src/model/default_model.rs 94.44% <100.00%> (-0.02%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 67e8500...5aa9255. Read the comment docs.

@xcaptain
Copy link
Contributor

xcaptain commented Apr 1, 2020

@hackerchai We don't need to change &str to String in parameters. https://hermanradtke.com/2015/05/03/string-vs-str-in-rust-functions.html
图片

Only change the returned &str is enough.

@GopherJ
Copy link
Member

GopherJ commented Apr 1, 2020

Hello @xcaptain, here I found a good post which explains why String is better than &str in some situations.

rust-lang/rust#46966
projectfluent/fluent-rs#123

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 Vec<String> from file system, network etc. We often just generate Vec<String> over Vec<&str>

@GopherJ
Copy link
Member

GopherJ commented Apr 1, 2020

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 csv file for getting default policies. I generated Vec<StringRecord> but it's not convenient to convert Vec<StringRecord> to Vec<&str>.

Then if I tried to do so, I'll got lifetime issue because the Vec<&str> will share the samle lifetime with generated StringRecord.

@xcaptain
Copy link
Contributor

xcaptain commented Apr 1, 2020

Ok, I agree to use owned string in all the public APIs.

@GopherJ
Copy link
Member

GopherJ commented Apr 1, 2020

Thanks @xcaptain and glad to see you agree with me!

@xcaptain
Copy link
Contributor

xcaptain commented Apr 1, 2020

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.

@DevinR528
Copy link
Contributor

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);

@xcaptain
Copy link
Contributor

xcaptain commented Apr 2, 2020

@DevinR528 You are right, my mistake, RFC 241 has been merged.

I was think to write a function that can take String and &str at the same time. The deref trait makes &String equals to &str but not very idea.

How about we use AsRef https://doc.rust-lang.org/std/convert/trait.AsRef.html here, then we don't need to dereference a string manually?

@GopherJ
Copy link
Member

GopherJ commented Apr 2, 2020

@xcaptain @DevinR528 I think Into<String> + Send + Sync can help.

It's a common pattern to use Into<String> in other library. In my opinion we don't need it because in the most cases we just simply generate Vec<String>.

It's hard to find a situation in which we generated Vec<&str>. If we do have then it may be hand written &str which avoided lifetime issue because they are 'static.

@DevinR528
Copy link
Contributor

@GopherJ as you know (you also tried to get the Api traits to take an Into<String>) it is way more trouble than it would be worth. I'm pretty sure to get any of the traits to use Into<String> we would have to Box it since its a dyn Into<string> and not boxing we may run into the same problem as with

pub struct Enforcer<A: Adaptor> {
    adaptor: A

where it only accepts one instance.

@GopherJ
Copy link
Member

GopherJ commented Apr 2, 2020

@DevinR528 Yes I agree, I removed static dispatcher because it causes some constraints.

We'll not be able to change types in runtime. (once instance is generated)

Anyway I think we should stick to Vec<String> before finding better solution

@GopherJ
Copy link
Member

GopherJ commented Apr 4, 2020

I think we can merge this. Thanks @hackerchai for your contributions!

@GopherJ GopherJ merged commit 3be55a1 into casbin:master Apr 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants