-
Notifications
You must be signed in to change notification settings - Fork 96
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
Conversation
f8e45aa
to
e910ccb
Compare
There was a problem hiding this 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.
e910ccb
to
6e90843
Compare
@@ -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 |
There was a problem hiding this comment.
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. =)
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 const
s 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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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 ?
i merged the enum version. |
CHANGES.md
if knowledge of this change could be valuable to users.OLCxxx GDAL
preprocessor defines toLayerCaps::OLCxxx
constants.OGR_L_TestGetCapability
asLayer::test_capability(&self, capability: LayerCaps::Type) -> Result<bool>