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

Implement wrapper for OGR_L_TestCapability #160

Closed
wants to merge 1 commit into from

Conversation

dmarteau
Copy link
Contributor

@dmarteau dmarteau commented Feb 4, 2021

  • I agree to follow the project's code of conduct.
  • I added an entry to CHANGES.md if knowledge of this change could be valuable to users.

  • Map OLCxxx GDAL preprocessor defines to LayerCaps::OLCxxx constants.
  • Add wrapper for OGR_L_TestGetCapability as Layer::test_capability(&self, capability: LayerCaps::Type) -> Result<bool>

Copy link
Contributor

@rmanoka rmanoka left a comment

Choose a reason for hiding this comment

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

Just a couple of minor suggestions.

src/vector/layer.rs Outdated Show resolved Hide resolved
@@ -101,6 +146,13 @@ impl<'a> Layer<'a> {
_string(rv)
}

pub fn has_capability(&self, capability: LayerCaps::Type) -> Result<bool> {
let rv = unsafe {
gdal_sys::OGR_L_TestCapability(self.c_layer, CString::new(capability)?.as_ptr()) == 1
Copy link
Member

Choose a reason for hiding this comment

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

When would this fail? If you do go the way of only accepting an enum of input strings, I'd argue you could unwrap here and offer a nicer API to your users: bool instead of Result<bool>.

I think we're better equipped to carefully scrutinize unwrap code centrally in a library like this, and save the attention of downstream users who now will have to ask "will their code explode" whenever they use this method.

Feel free to ignore this suggestion, reasonable minds may disagree. =)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'd love to get rid of the Result<>, but the input type is actually a alias to '&'static str' so it does not prevent to use invalid candidate input that may fail the conversion to CString. If everybody think that the intention notice is ok, I will happily go with unwrap.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think unwrap() in the present implementation is acceptable. I believe what @michaelkirk suggested is to accept a enum as input rather than &str in which case we could even get rid of the &str -> &Cstr conversion. That is a lot of boilerplate (unless there's a nice macro crate), so we could postpone it to a later PR.

Copy link
Member

Choose a reason for hiding this comment

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

If the input type doesn't forbid passing in something that would explode, I would personally be in favor of keeping the Result as is.

Copy link
Contributor

Choose a reason for hiding this comment

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

So, if we could somehow define the consts in mod LayerCaps directly as &'static Cstr, then we can avoid both the extra allocation to convert &str to &Cstr, as well as the Result<>. However, it seems to need some complicated macro: see SO post.

Copy link
Member

Choose a reason for hiding this comment

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

Oh wow, good find. Looks fun.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Implemented the enum alternative solution, so one may compare.

Copy link
Contributor Author

@dmarteau dmarteau Feb 4, 2021

Choose a reason for hiding this comment

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

The enum alternative does not avoid the conversion but limit the input to acceptable values so conversion to CStr will not return an error.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks for putting that together @dmarteau! I personally prefer the #162 version.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So, should we close that PR in favor of #162 ?

@jdroenner
Copy link
Member

i merged the enum version.

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