yewstack / implicit-clone Goto Github PK
View Code? Open in Web Editor NEWImmutable types and ImplicitClone trait similar to Copy
Immutable types and ImplicitClone trait similar to Copy
Hello,
I'm trying to figure out how I can use the library correctly. Here's what I'm trying to do:
use implicit_clone::ImplicitClone;
use std::{cell::RefCell, rc::Rc};
#[derive(Clone)]
struct A {
num: i32,
}
impl ImplicitClone for A {}
struct B<T>(Rc<RefCell<T>>);
impl<T> Clone for B<T> {
fn clone(&self) -> Self {
Self(self.0.clone())
}
}
impl<T> ImplicitClone for B<T> {}
fn do_stuff(val: B<A>) {
val.0.borrow_mut().num = 200;
}
fn do_stuff_2<F: Fn()>(closure: F) {
closure();
}
fn main() {
let a = B(Rc::new(RefCell::new(A { num: 100 })));
do_stuff(a);
do_stuff_2(|| a.0.borrow_mut().num = 200);
println!("{}", a.0.borrow().num);
}
This results in a compiler error:
error[E0382]: borrow of moved value: `a`
--> src/main.rs:32:16
|
30 | let a = B(Rc::new(RefCell::new(A { num: 100 })));
| - move occurs because `a` has type `B<A>`, which does not implement the `Copy` trait
31 | do_stuff(a);
| - value moved here
32 | do_stuff_2(|| a.0.borrow_mut().num = 200);
| ^^ --- borrow occurs due to use in closure
| |
| value borrowed here after move
When I change this to explicit clones:
do_stuff(a.clone());
do_stuff_2(|| a.clone().0.borrow_mut().num = 200);
It works as expected and prints out 200
. Am I doing something wrong?
Using version implicit-clone = "0.3.1"
and rust edition = "2021"
.
Thanks for help!
these basic functions may be useful to include with the library
impl IArray<A> {
fn push(&self, item: A) -> IArray<A>;
fn insert(&self, item: A, pos: usize) -> IArray<A>;
}
impl IMap<K, V> {
fn upsert(&self, key: K, value: V> -> IMap<K, V>;
}
Would be useful, for more String compatibility
Issue for implementing ImplicitClone
for all Copy
types.
As noted in yewstack/yew#3464 (comment), the entirety of all Copy
types may receive an ImplicitClone
implementation via specializations (currently nightly):
#![feature(specialization)]
default impl<T: Copy> ImplicitClone for T {
}
However, the RFC for specializations is nowhere near being done, so for this point in time ImplicitClone
will still stay limited in the amount of types that will be implemented (and this issue will stay open).
Alternatively, a Rust RFC should be made that would allow ImplicitClone
for Copy
types: the only blocking suggestion from the Rust compiler is that the trait may have colliding implementations in future, which is not correct since the marker trait is empty, and there is nothing to collide with. The RFC would allow repeating marker trait implementations. Comment if someone makes such an RFC and I will update this issue.
I'm not sure what uses this library fills exactly. Could you elaborate further on potential uses and explain them?
https://doc.rust-lang.org/std/str/trait.FromStr.html
reason: both number and IString are widely used for vaule of form input, but number types only impl FromStr, not FromString or TryFromString. If IString impl FromStr (just like String), we can write something like:
fn input_onchange<T: FromStr + 'static>(setter: UseStateSetter<T>) -> Callback<Event> {
str implements this - IString should as well. In particular, IStrings cannot be used as BTreeMap keys or BTreeSet items without this.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.