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 various methods. #38

Merged
merged 28 commits into from
Sep 20, 2018
Merged

Implement various methods. #38

merged 28 commits into from
Sep 20, 2018

Conversation

ASalvail
Copy link
Collaborator

@ASalvail ASalvail commented Sep 10, 2018

Best reviewed per commit.

@ASalvail ASalvail changed the title [WIP] Implement various methods. Implement various methods. Sep 16, 2018
@ASalvail
Copy link
Collaborator Author

Addresses a lot of #30 points.
What's left is everything that handles paths and a way to represent whatever look methods return.

Those will be separate PRs however, so this one should stand on its own.

Copy link
Collaborator

@daboross daboross left a comment

Choose a reason for hiding this comment

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

First pass on this!

Overall I'm super glad to have these implemented, and the majority of them look all good. There are a few ones which I think could be changed, and those are noted in the review.

Hope it's alright that I've just commented on the final changeset rather than the individual commits for each part.

Thanks!

@@ -775,10 +821,59 @@ impl ResourceType {
CatalyzedGhodiumAlkalide => 90,
}
}

pub fn to_string(&self) -> String {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd prefer to use the JS methods present at https://github.com/daboross/screeps-in-rust-via-wasm/blob/master/screeps-game-api/javascript/utils.js#L97 to do variant -> string conversion instead of converting to a string inside Rust wherever possible.

It's mostly for efficiency as translating strings across the rust/js boundary is much more expensive than simple integers.

Sorry about not documenting the use of /rational for those methods better!


If we do want this method anyways, I think it could be changed to return &'static str rather than String? All of the strings here are static so it might be a bit wasteful to allocate a new string per use. If we do that renaming it to as_str like mentioned below might be a good idea.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Argh, I forgot about those!
I don't think having the string instead of the variant is useful. It was only meant to convert to a string before passing the hand to JS.

How about a wrapper around those JS functions that would implement From<ResourceType> for Value::String and TryFrom<Value::String> for ResourceType using those js methods instead of the usual conversion provided from stdweb?

Copy link
Collaborator

Choose a reason for hiding this comment

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

The problem with TryFrom<Value::String> is that at that point the string has already been loaded into Rust, so it's equivalent to doing a method like this.

Value::String just wraps a rust String, not a pointer to a JS string. If we construct it or consume it at all we're already paying the cost of transfering the string between Rust and JS, which is what I want to avoid.

@@ -60,7 +60,7 @@ pub mod cpu {
///
/// [http://docs.screeps.com/api/#Game.getHeapStatistics]: http://docs.screeps.com/api/#Game.getHeapStatistics
///
/// Returns object with all 0 values if heap statistics are not available.
/// Returns object with all 0 values if heap statistics are not availe.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Accidentally included?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yup, thanks

@@ -170,12 +172,185 @@ pub mod map {

/// See [http://docs.screeps.com/api/#Game.map.isRoomAvailable]
///
/// [http://docs.screeps.com/api/#Game.map.isRoomAvailable]: http://docs.screeps.com/api/#Game.map.isRoomAvailable
/// [http://docs.screeps.com/api/#Game.map.isRoomAvaile]: http://docs.screeps.com/api/#Game.map.isRoomAvaile
Copy link
Collaborator

Choose a reason for hiding this comment

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

Accidentally included?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

🤔 now those I think I corrected at some point, they might have reverted.... somehow 😕

/// Unimplemented
#[allow(unused_variables)]
pub fn look_at(&self, x: u32, y: u32) -> ! {
unimplemented!()
Copy link
Collaborator

Choose a reason for hiding this comment

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

See above comment on unimplemented!().

/// Unimplemented
#[allow(unused_variables)]
pub fn look_at_area(&self, top: u32, left: u32, bottom: u32, right: u32) -> ! {
unimplemented!()
Copy link
Collaborator

Choose a reason for hiding this comment

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

See above comment on unimplemented!().

impl TryFrom<String> for ResourceType {
type Error = String;

fn try_from(s: String) -> Result<Self, Self::Error> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

See comment on to_string: I'd prefer to use integers to translate the various types across the rust<->js boundary whenever possible.

Not against having this implemented, but I think it'd be good to at least mention the str->int translations available on the JS side in a doc-comment here, and to warn about not using this if not necessary.


#[allow(unused_variables)]
pub fn find_route(from_room: Room, to_room: Room, route_callback: Option<impl Fn(&str, &str) -> u32>) -> !{
unimplemented!()
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd rather leave these methods out for the sake of having compile-time errors rather than runtime ones when trying to use them.

We could leave it in here commented out for the sake of keeping the signature, though.

Thoughts?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I like having them in the code because of the documentation generated (granted, this one doesn't have a doc).
I believed that using a function with the never return type wouldn't compile. After testing... it's annoying and confusing, so commenting it out seems like the best option.

Copy link
Collaborator

Choose a reason for hiding this comment

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

We could also have an unimplemented-functions feature which we enable when building docs but disable otherwise.

Or we could also change the function to accept a never/void input parameter '_unimplemented'! Never type in the return position can actually be called, it just guarantees the function will only ever panic or loop forever. If we include something like enum UnimplementedParameterVoid {} and then take _unimplemented: UnimplementedParameterVoid that would prevent anyone from calling it since no one would be able to construct that value.

}

impl OrderType {
fn as_string(&self) -> String {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since we are converting to a different type, to_string would be more appropriate than as_string.

See https://rust-lang-nursery.github.io/api-guidelines/naming.html#ad-hoc-conversions-follow-as_-to_-into_-conventions-c-conv.

In addition, returning &'static str could be better here. Though if we do that, as_str might be a more appropriate name after all - it would indeed be free.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Would you rather we apply the same principle as for the resources and pass an integer instead? Again, this is solely used to call into JS, otherwise, the type is more than explicit enough.

Copy link
Collaborator

Choose a reason for hiding this comment

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

If you think it's sane, sure!

I'm not 100% sure this optimization is the best idea, but then again it does seem worth it without much cost (just writing JS code to do the string->variant conversion instead of Rust code). I originally added it for resource types just because I imagined I would use those a lot. If it wouldn't be too much trouble an __order_type_num_to_str and __order_type_str_to_num pair seems like it'd be beneficial to me.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Sorry, I think that was a bit rambly/not clear.

I'd go ahead with this and apply the same principles. I'm not completely sure that it's the best idea, but I like it anyways, and I'm confident it will make the conversions faster.

}
}

pub fn deal(order_id: &str, amount: u32, target_room: Room) -> ReturnCode {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could this take &Room instead?

I'd almost want to have it taking a room name &str so that someone could execute deals without grabbing a reference to their room, but I don't feel too strongly about that.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I do, it was a mistake on my part. Fixed!

@@ -38,10 +45,19 @@ impl Creep {
js_unwrap!(@{self.as_ref()}.moveTo(@{x}, @{y}))
}

#[allow(unused_variables)]
pub fn move_by_path(path: String) -> ! {
unimplemented!()
Copy link
Collaborator

Choose a reason for hiding this comment

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

See above comment on unimplemented!().

@ASalvail
Copy link
Collaborator Author

Thanks a lot for taking the time to read through these.

I addressed most small things. I'll wait for your feedback on the points raised above before finishing the corrections.

@daboross
Copy link
Collaborator

I'm glad to do it- thank you for doing this huge implementation pr!

I've replied to the individual threads.

daboross added a commit that referenced this pull request Sep 18, 2018
js_unwrap! is not updated as it's not touched by this commit and will
be fixed by #38.
daboross added a commit that referenced this pull request Sep 18, 2018
js_unwrap! is not updated as it's not touched by this commit and will
be fixed by #38.
@ASalvail
Copy link
Collaborator Author

There you go, that should address all of your concerns 😃

@ASalvail
Copy link
Collaborator Author

Addresses most of #23

@daboross
Copy link
Collaborator

LGTM! 👍

I think travis is complaining because of changes on the master branch so fixing that after merging seems sanest.

Opened #52 for one lingering issue that I think could easily be dealt with separately.

@daboross daboross merged commit e3dc452 into rustyscreeps:master Sep 20, 2018
}

// impl OrderType {
// fn as_string(&self) -> String {
Copy link
Collaborator

Choose a reason for hiding this comment

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

If you do want to keep this method in, I don't think it would hurt.

If we want to add in a typed OrderType to the transactions, the serde translation code would have to use strings anyways since stdweb turns it into JSON before loading it in.

daboross added a commit that referenced this pull request Sep 25, 2018
js_unwrap! is not updated as it's not touched by this commit and will
be fixed by #38.
daboross added a commit that referenced this pull request Sep 25, 2018
js_unwrap! is not updated as it's not touched by this commit and will
be fixed by #38.
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