我有以下特点:
package com.czhou.dailyprincetoniannewspaper;
import android.os.Bundle;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import java.util.ArrayList;
import java.util.List;
/**
* Created by czhou on 11/21/2015.
*/
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
private ProgressBar mProgressBar;
private CoordinatorLayout coordinatorLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager){
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new PUNewsFragment(), "News");
adapter.addFragment(new PUSportsFragment(), "Sports");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
我想创建另一个特性,它在一个函数中采用一个实现pub trait Stream<T: Read + Write + AsRawFd>: Sized {
fn new(stream: T) -> Result<Self, Error>;
fn recv(&mut self) -> Result<Vec<u8>, Error>;
fn send(&mut self, buf: &[u8]) -> Result<usize, Error>;
}
作为参数的类型,如下所示:
Stream
我已经尝试了几种变体来定位特征定义,但我似乎总是得到错误:
pub trait EventHandler<T: Stream> {
fn on_data_received(&mut self, stream: T, buffer: Vec<u8>);
fn on_stream_closed(&mut self, id: u32);
}
这个的正确语法是什么?
答案 0 :(得分:3)
您需要在<T: Stream>
界限上指定一个类型参数,如下所示:
pub trait EventHandler<T: Stream<U>, U: Read + Write + AsRawFd> {
fn on_data_received(&mut self, stream: T, buffer: Vec<u8>);
fn on_stream_closed(&mut self, id: u32);
}
您还可以使用where
子句编写边界:
pub trait EventHandler<T, U> where
T: Stream<U>,
U: Read + Write + AsRawFd
{
fn on_data_received(&mut self, stream: T, buffer: Vec<u8>);
fn on_stream_closed(&mut self, id: u32);
}
另一种选择是使用相关类型。像这样定义Stream
:
pub trait Stream: Sized {
type T: Read + Write + AsRawFd;
fn new(stream: Self::T) -> Result<Self, Error>;
fn recv(&mut self) -> Result<Vec<u8>, Error>;
fn send(&mut self, buf: &[u8]) -> Result<usize, Error>;
}
然后您EventHandler
的原始定义将起作用。