是否可以克隆类型为std::ops::Generator
的变量?有一个生成器,我想创建一个具有干净堆栈的新实例,没有clone()
方法。我在Box
和Pin
内部进行了尝试,但是它不会克隆!
#![feature(generators, generator_trait)]
fn main() {
let gen = move || yield ();
let gen1 = gen.clone();
let gen = Box::new(move || yield ());
let gen1 = gen.clone();
let gen = Box::pin(move || yield ());
let gen1 = gen.clone();
}
error[E0599]: no method named `clone` found for type `[generator@src/main.rs:4:15: 4:31 _]` in the current scope
--> src/main.rs:6:20
|
6 | let gen1 = gen.clone();
| ^^^^^
据我了解,闭包可以被克隆;发电机怎么了?
我发现的唯一解决方法是从某个函数返回生成器(然后每次都会返回新生成器),但这在语法上给我带来了不便。
答案 0 :(得分:2)
不,它是not possible to clone a generator。
当前的解决方法是每次需要重置时都构造一个新的生成器。
public class HomeActivity extends AppCompatActivity { private FirebaseAuth mAuth; private Toolbar mToolbar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_home); FirebaseApp.initializeApp(this); mToolbar =findViewById(R.id.toolbarId); getSupportActionBar().setTitle("PhorumChat"); } private void sendToStart() { Intent goToWelcomePage = new Intent(HomeActivity.this, WelcomeActivity.class); startActivity(goToWelcomePage); finish(); } @Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); getMenuInflater().inflate(R.menu.main_menu, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { super.onOptionsItemSelected(item); if (item.getItemId() == R.id.main_logoutBtn){ FirebaseAuth.getInstance().signOut(); sendToStart(); } return true; } @Override public void onStart() { super.onStart(); // Check if user is signed in (non-null) and update UI accordingly. FirebaseUser currentUser = mAuth.getCurrentUser(); updateUI(currentUser); } private void updateUI(FirebaseUser currentUser) { if (currentUser ==null){ Intent homepage= new Intent(HomeActivity.this, WelcomeActivity.class); startActivity(homepage); finish(); } }
类型的变量
这些不是该类型的变量,而std::ops::Generator
是类型|| foo()
的变量。 Fn()
是特征,而Generator
是类型。您的dyn Generator
变量是实现特征gen
的匿名类型。
另请参阅: