GithubHelp home page GithubHelp logo

implicit-clone's People

Contributors

cecton avatar colonelthirtytwo avatar dependabot[bot] avatar futursolo avatar hamza1311 avatar kirillsemyonkin avatar uzytkownik avatar xx avatar yozhgoor avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

implicit-clone's Issues

How to use the library?

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!

Immutable collection operations

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>;
}

`ImplicitClone` for `Copy` types

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.

Usecases

I'm not sure what uses this library fills exactly. Could you elaborate further on potential uses and explain them?

impl FromStr for IString

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> {

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.