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

Use mutable references as per semantics #161

Merged
merged 10 commits into from
Apr 25, 2021
Merged

Conversation

rmanoka
Copy link
Contributor

@rmanoka rmanoka 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.

This builds on #158 and also adds quite a few breaking changes.

closes #159
closes #150

@rmanoka
Copy link
Contributor Author

rmanoka commented Feb 4, 2021

In this PR we add a (commented out) test that ensures that the semantics we enforce indeed leads to a compile error if used incorrectly:

fn test_features_mut_lifetime_enforce() {
    with_layer("roads.geojson", |mut layer| {
        for _ in layer.features() {
            // the next line is a compile error as layer is mut borrowed above
            let _ = layer.defn();
        }
    });
}

Should we add trybuild as a dev-dependency so that we can express this as a regular test? The crate does add a few heavy deps (syn, rustc), but it is only a dev dependency and may be okay. I think it'll be useful in this crate as we try to ensure borrow semantics from an underlying library that doesn't have those.

@jdroenner
Copy link
Member

it would be fine for me

@michaelkirk
Copy link
Member

This makes a lot of sense to me - I think it's good to have the compiler enforce "rust like" API usage.

Taking it a step further, do you think it would be worthwhile for things like:

pub fn layer_by_name(&self, name: &str) -> Result<Layer> {

To propagate some kind of lifetime:

pub fn layer_by_name<'a>(&'a self, name: &str) -> Result<Layer<'a>> {

To clarify that &self is still "borrowed" as long as Layer exists?

@rmanoka
Copy link
Contributor Author

rmanoka commented Feb 4, 2021

@michaelkirk

To clarify that &self is still "borrowed" as long as Layer exists?

I think it is already implicitly inferred. See Dataset::child_layer which connects it to the Layer.

+ add `trybuild`

+ add test to verify `Layer::features` mut-borrow. Refer
  georust#159
@michaelkirk
Copy link
Member

Oh you're absolutely right, sorry.

@michaelkirk michaelkirk mentioned this pull request Feb 22, 2021
2 tasks

#[test]
fn test_features_aliasing_compile_fail() {
let t = trybuild::TestCases::new(); // A compilation test that should fail.
Copy link
Member

Choose a reason for hiding this comment

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

Cool to see trybuild!

I've never seen tests like this outside of the actual compiler dev, but it makes sense!

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 a bit of a heavy dep., but seems useful for this crate as we try to impose rust semantics on ffi libs. I figure we could always comment it out, and remove the deps if CI takes too long.

Copy link
Member

@jdroenner jdroenner left a comment

Choose a reason for hiding this comment

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

i think we should merge it with the trybuild. I have only one question left :)

#[test]
fn test_features_aliasing_compile_fail() {
let t = trybuild::TestCases::new(); // A compilation test that should fail.
t.compile_fail("tests/ui/01-features-aliasing-errors.rs");
Copy link
Member

Choose a reason for hiding this comment

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

why is the tests folder called "ui"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure; I just followed the instructions from the the trybuild crate docs. Not having the ui/ folder caused some errors as (I think) cargo tried to run them as normal tests, and failed. I didn't try a different folder name though.

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 seems that any folder inside tests/ works; have renamed it to compile-tests

@jdroenner
Copy link
Member

uh i broke it by merging the other PR -_-

@rmanoka
Copy link
Contributor Author

rmanoka commented Mar 10, 2021

@jdroenner Not sure what the failure is, but I'll take a closer look next week and try to resolve it.

@jdroenner jdroenner mentioned this pull request Apr 25, 2021
1 task
jdroenner added a commit that referenced this pull request Apr 25, 2021
@jdroenner jdroenner merged commit 3fe326a into georust:master Apr 25, 2021
@michaelkirk michaelkirk mentioned this pull request Dec 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants