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

Initial concepts of type safe API #2

Open
wants to merge 31 commits into
base: master
Choose a base branch
from
Open

Initial concepts of type safe API #2

wants to merge 31 commits into from

Conversation

0x7CFE
Copy link
Member

@0x7CFE 0x7CFE commented May 9, 2017

No description provided.

@0x7CFE 0x7CFE assigned kpp and unassigned kpp May 9, 2017
@0x7CFE 0x7CFE requested a review from kpp May 9, 2017 13:57
Copy link
Member

@kpp kpp left a comment

Choose a reason for hiding this comment

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

Класс 👍

src/player.rs Outdated
pub struct Player(*mut sys::Player);

impl Player {
pub fn get_name(&self) -> BwString {
Copy link
Member

Choose a reason for hiding this comment

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

Зачем BwString?

Copy link
Member Author

Choose a reason for hiding this comment

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

Пока не могу внятно ответить, разбираюсь с интерфейсами и пытаюсь осознать, какие контракты нам будут нужны.

Если сходу конвертировать в String будет еще одна аллокация. Вообще можно использовать OsString который является промежуточным звеном между сырыми строками и строками Rust, и может быть дешево преобразован в обе стороны при условии, что внутри лежит UTF-8.

Copy link
Member

Choose a reason for hiding this comment

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

Дим, наверно пофиг на доп аллокацию? get_name не должны спрашивать чаще 16 раз за такт

Copy link
Member Author

@0x7CFE 0x7CFE May 10, 2017

Choose a reason for hiding this comment

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

Не то что бы это было критично. Просто зачем?

Я реализовал Deref<Target=str> и AsRef<str> для этого типа, то есть его можно спокойно подставлять в те функции, где ожидается &str. Если же потребуется владение, то или x.to_owned() или String::from(x).

src/string.rs Outdated
}
}

pub fn data(&self) -> &CStr {
Copy link
Member

Choose a reason for hiding this comment

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

Зачем тут такие кишки?

Copy link
Member Author

@0x7CFE 0x7CFE May 9, 2017

Choose a reason for hiding this comment

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

В тесте показаны примеры использования. А вообще все будет зависеть от того, как их потом использовать.

Copy link
Member Author

Choose a reason for hiding this comment

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

Если строку придется отдавать обратно, например в Game::sendText то лучше иметь возможность получить доступ к оригинальному буферу. Это преобразование дешевое. Семантически, CStr это слайс сишной строки.

pub fn distance_to<T>(&self, t: &T) -> i32
where T: HasPosition
{
unsafe { sys::Unit_getDistance_Position(self.raw, t.position()) }
Copy link
Member

Choose a reason for hiding this comment

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

See the difference in implementation:

int UnitInterface::getDistance(Position target) const
    // retrieve left/top/right/bottom values for calculations
    int left = target.x - 1;
    int top = target.y - 1;
    int right = target.x + 1;
    int bottom = target.y + 1;

vs

  int UnitInterface::getDistance(Unit target) const
    // retrieve left/top/right/bottom values for calculations
    int left = target->getLeft() - 1;
    int top = target->getTop() - 1;
    int right = target->getRight() + 1;
    int bottom = target->getBottom() + 1

Where

  int UnitInterface::getLeft() const
  {
    return this->getPosition().x - this->getType().dimensionLeft();
  }

@ttdonovan
Copy link

Are the examples expected to work? Running the session_lifetime.dll crashes in my environment. I was able to successfully get the example Dll.dll from bwapi-c to work.

@dkashitsyn
Copy link

Yes, it is a known problem that need to be solved. Regression appeared in between e3b4c2c .. 4a0596f. If you just want to check it out, you may use revision 9ff5fce.

@kpp kpp force-pushed the bwapi-c branch 2 times, most recently from 635df27 to d452b00 Compare July 11, 2018 14:50
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.

None yet

4 participants