我在给一个struct一个成员变量时遇到了麻烦,该变量是对另一个类型的引用.这是我的结构和实现:
struct Player<'a> {
current_cell: &'a Cell,
}
impl<'a> Player<'a> {
pub fn new(starting_cell: &'a Cell) -> Player<'a> {
Player { current_cell: starting_cell }
}
}
Run Code Online (Sandbox Code Playgroud)
玩家可以参考Cell他们所处的当前状态.这是我的Game结构及其实现:
struct Game {
is_running: bool,
cells: Vec<Cell>,
}
impl Game {
pub fn new() -> Game {
let cells = construct_cells();
let player = Player::new(cells[0]);
Game {
is_running: false,
cells: cells,
}
}
}
Run Code Online (Sandbox Code Playgroud)
cells是Cells 的向量.当我创建游戏时,我创建了一个单元格向量,construct_cells()然后我在第一个单元格中启动了玩家.我得到的错误是:
expected &Cell, found struct `Cell`
Run Code Online (Sandbox Code Playgroud)
我可以看到我在创建时没有传递引用Player,但是如果我将参数更改为 …
我的应用程序使用ViewPager的一个活动来交换应用程序页面的片段.每个其他片段都有效,但出于某种原因,其中一个片段,我的设置片段,没有.下图说明了这一点.红色区域是不需要的边距.蓝色区域是在主活动布局中定义的工具栏 - 它始终存在.绿色区域是主要的ViewPager,也在主要活动布局中定义:
这就是MainActivity布局的样子:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.MainActivity"
android:fitsSystemWindows="true" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Tab Bar -->
<com.rentalapp.rentmi.views.SlidingTabLayout
android:id="@+id/main_content_pager_tab_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/toolbar"
android:background="@color/primary" />
<!-- Content Pager -->
<android.support.v4.view.ViewPager
android:id="@+id/main_content_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/main_content_pager_tab_bar" />
</RelativeLayout>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
这就是Settings片段的样子:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Settings"
android:id="@+id/textView2"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:paddingTop="10dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Log Out"
android:id="@+id/logout"
android:layout_below="@+id/textView2"
android:layout_centerHorizontal="true"
android:paddingTop="10dp" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
如何修复这个不必要的保证金?如果我android:fitsSystemWindows="true"从主布局中删除,那么每个其他页面都会在标签栏上绘制通知栏,这是我不想要的.
根据我对TCP的理解,如果我将X字节发送到套接字,它几乎可以保证它会到达那里,尽管有效负载可能会分裂并到达两个独立的部分(所以如果我在等待一个25字节的数据包我可能得到20个字节,然后在下一次读取时再增加5个字节.我从来没有见过这种情况,但是我希望能够被覆盖.
在过去,我通常设计我的套接字读取来解释这一点 - 将传入的字节读入缓冲区,然后不断检查缓冲区的大小.如果有足够的数据用于整个数据包,请处理该数据包,从缓冲区前面删除字节,然后继续.
我现在正在尝试使用mioTCP套接字在Rust中编写类似的系统.我的缓冲区只是一个数组:[u8; MAX_BUFFER_SIZE]这是我的套接字读取代码:
loop {
// Read until there are no more incoming bytes
match socket.read(&mut buffer) {
Ok(0) => {
// Socket is closed, Client has disconnected!
// (perform disconnection here)
break;
},
Ok(read_bytes) => {
println!("Read {} bytes from client", read_bytes);
},
Err(e) => {
if e.kind() == io::ErrorKind::WouldBlock {
// Socket is not ready anymore, stop reading
break;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这不支持读取任何拆分数据,因为该read函数只是覆盖缓冲区开头的数据,而不是将每个后续调用附加到结尾.在C#和C++中,有一个偏移参数可以提供给等效调用以允许这种行为,但我无法弄清楚如何使用它mio.我找不到任何偏移参数的事实让我相信我在理解read函数时遗漏了一些重要的东西.
我如何编程插座读取来解释这个?