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

Improve tag api #25

Merged
merged 3 commits into from
Jan 10, 2020
Merged

Improve tag api #25

merged 3 commits into from
Jan 10, 2020

Conversation

GuillaumeGomez
Copy link
Contributor

No description provided.

Copy link
Owner

@TeXitoi TeXitoi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The doc improvements are great.

src/objects.rs Outdated
/// Returns the key associated value or `None` if there is no such tag.
pub fn get(&self, key: &str) -> Option<&str> {
self.0.get(key).map(|v| v.as_str())
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get is already usable, returning a Option<&String>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's easy to turn a String into a &str, however it's very annoying to have in one code &"s".to_owned(). So I definitely prefer it this way. :)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Give me the context where this method is way better than the already existing one, please.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's very useful in case you don't want to deconstruct a type. For example:

fn foo(s1: &String) {
    Some(s1) == Some("b"); // doesn't work!
}

fn main() {
    foo(&"a".to_owned());
}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the purpose of contains

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to add a doccomment on Tags saying that it Deref to a Map<String, String>?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too sad there is not the "Methods from Deref<Target = TargetImpl>" as in the official documentation https://doc.rust-lang.org/std/string/struct.String.html

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh so I wasn't completely crazy! I'll check if I can make it visible. But I still think that we should remove contains.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I still think that we should remove contains.

I think we can't agree on that ;-)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, you're the maintainer so I'll leave it there. But please note that I complained about it. :)

src/objects.rs Outdated
}
/// Returns `true` if contains a tag with the given `key`.
pub fn contains(&self, key: &str) -> bool {
self.0.get(key).is_some()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, because:

  • that's a breaking change
  • that's not the idiomatic API: you can already do tags.contains_key("foo")

This contains method is here because that's something you want a lot, and is easier to write than your get variant. It follows the Set interface.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's a breaking change

I changed the medium version of the crate.

that's not the idiomatic API: you can already do tags.contains_key("foo")

That's a very good point, it should indeed be contains_key. Documentation failed me.

This contains method is here because that's something you want a lot, and is easier to write than your get variant. It follows the Set interface.

I don't agree with this assumption: having a get is more idiomatic. contains is misleading in my opinion (that's mainly why I didn't think of contains_key).

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You already have get and contains_key via Deref

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then this method shouldn't be there.

@GuillaumeGomez
Copy link
Contributor Author

Ok, deref methods now appear in the documentation.

@GuillaumeGomez
Copy link
Contributor Author

GuillaumeGomez commented Jan 8, 2020

I also made a minor crate version change instead of a medium one.

@@ -17,30 +17,33 @@ use std::ops::{Deref, DerefMut};
/// tags](http://wiki.openstreetmap.org/wiki/Tags) for more
/// information.
#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct Tags(TagsImpl);
/// FlatMap representing the key-value pairs of the tags
pub type TagsImpl = ::flat_map::FlatMap<String, String>;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You must let this type alias, else it's a breaking change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't change the API, so from my point of view it isn't one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All codes using your crate won't break with this change.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will if someone use something like struct Data { tags: osmpbfreader::objects::TagsImpl }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Do you mind if we remove it then?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to keep it, but no strong feeling on it (I doubt someone is using it).

pub struct Tags(TagsImpl);
/// FlatMap representing the key-value pairs of the tags
pub type TagsImpl = ::flat_map::FlatMap<String, String>;
pub struct Tags(::flat_map::FlatMap<String, String>);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just not using the type alias here would be enough to have the documentation. Looks like a bug, no?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rust-lang/rust#68016

And the alias is useless, unless I'm missing something?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The alias is more here for backcompatibility, before, this TagsImpl was a new type if I recall well.

@TeXitoi TeXitoi merged commit 940c435 into TeXitoi:master Jan 10, 2020
@TeXitoi
Copy link
Owner

TeXitoi commented Jan 10, 2020

v0.13.3 published.

@GuillaumeGomez GuillaumeGomez deleted the improve-tag-api branch January 10, 2020 08:41
@GuillaumeGomez
Copy link
Contributor Author

Thanks! :)

@GuillaumeGomez
Copy link
Contributor Author

I opened the PR to rustdoc as well: rust-lang/rust#68093

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.

2 participants