clone

How to clone a struct storing a boxed trait object?

南楼画角 提交于 2019-12-17 03:16:31
问题 I wrote a program that has the trait Animal and the struct Dog implementing the trait. It also has a struct AnimalHouse storing an animal as a trait object Box<Animal> . trait Animal { fn speak(&self); } struct Dog { name: String, } impl Dog { fn new(name: &str) -> Dog { return Dog { name: name.to_string(), }; } } impl Animal for Dog { fn speak(&self) { println!{"{}: ruff, ruff!", self.name}; } } struct AnimalHouse { animal: Box<Animal>, } fn main() { let house = AnimalHouse { animal: Box:

How to convert a Git shallow clone to a full clone?

耗尽温柔 提交于 2019-12-17 02:54:09
问题 Follow-up of this so-question: if I have a shallow clone, how to fetch all older commits to make it a full clone? 回答1: You can run git fetch --depth=1000000 (assuming the repository has less than one million commits). 回答2: The below command (git version 1.8.3) will convert the shallow clone to regular one git fetch --unshallow Then, to get access to all the branches on origin (thanks @Peter in the comments) git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*" git fetch origin

Any way to clone HTML5 canvas element with its content?

纵饮孤独 提交于 2019-12-17 02:47:27
问题 Is there any way to create a deep copy of a canvas element with all drawn content? 回答1: Actually the correct way to copy the canvas data is to pass the old canvas to the new blank canvas. Try this function. function cloneCanvas(oldCanvas) { //create a new canvas var newCanvas = document.createElement('canvas'); var context = newCanvas.getContext('2d'); //set dimensions newCanvas.width = oldCanvas.width; newCanvas.height = oldCanvas.height; //apply the old canvas to the new one context

Does calling clone() on an array also clone its contents?

筅森魡賤 提交于 2019-12-17 02:44:16
问题 If I invoke clone() method on array of Objects of type A, how will it clone its elements? Will the copy be referencing to the same objects? Or will it call (element of type A).clone() for each of them? 回答1: clone() creates a shallow copy. Which means the elements will not be cloned. (What if they didn't implement Cloneable ?) You may want to use Arrays.copyOf(..) for copying arrays instead of clone() (though cloning is fine for arrays, unlike for anything else) If you want deep cloning, check

How can you clone a WPF object?

本秂侑毒 提交于 2019-12-17 02:33:10
问题 Anybody have a good example how to deep clone a WPF object, preserving databindings? The marked answer is the first part. The second part is that you have to create an ExpressionConverter and inject it into the serialization process. Details for this are here: http://www.codeproject.com/KB/WPF/xamlwriterandbinding.aspx?fid=1428301&df=90&mpp=25&noise=3&sort=Position&view=Quick&select=2801571 回答1: The simplest way that I've done it is to use a XamlWriter to save the WPF object as a string. The

clone() vs copy constructor vs factory method?

徘徊边缘 提交于 2019-12-17 00:40:16
问题 I did a quick google on implementing clone() in Java and found: http://www.javapractices.com/topic/TopicAction.do?Id=71 It has the following comment: copy constructors and static factory methods provide an alternative to clone, and are much easier to implement. All I want to do is make a deep copy. Implementing clone() seems to make a lot of sense, but this highly google ranked article makes me a bit afraid. Here are the issues that I've noticed: Copy constructors don't work with Generics.

How to properly override clone method?

被刻印的时光 ゝ 提交于 2019-12-16 19:57:48
问题 I need to implement a deep clone in one of my objects which has no superclass. What is the best way to handle the checked CloneNotSupportedException thrown by the superclass (which is Object )? A coworker advised me to handle it the following way: @Override public MyObject clone() { MyObject foo; try { foo = (MyObject) super.clone(); } catch (CloneNotSupportedException e) { throw new Error(); } // Deep clone member fields here return foo; } This seems like a good solution to me, but I wanted

How can I change the default behavior of console.log in Safari?

烂漫一生 提交于 2019-12-16 19:17:42
问题 In Safari with no add-ons, console.log will show the object at the last state of execution, not at the state when console.log was called. I have to clone the object just to output it via console.log to get the state of the object at that line. Example: var test = {a: true} console.log(test); // {a: false} test.a = false; console.log(test); // {a: false} 回答1: I think you're looking for console.dir() . console.log() doesn't do what you want because it prints a reference to the object, and by

super.clone() operation not works in Derived Class

谁说胖子不能爱 提交于 2019-12-14 03:56:40
问题 This is raised because of the technical difficulties faced in my Project. Problem: I need to clone a Object of a Class where it extended the properties(Inheritance) from a third party library class(where we don't have access to modify its contents) Let me explain with example below: Parent Class: public class UnChangeableBaseClass { //fields and Methods } Child Class: class DerivedLocalClass extends UnChangeableBaseClass implements Cloneable { // local fields and methods public Object clone()

Cloning an array with its content

↘锁芯ラ 提交于 2019-12-14 03:40:12
问题 I want to make a copy of an array, to modify the copy in-place, without affecting the original one. This code fails a = [ '462664', '669722', '297288', '796928', '584497', '357431' ] b = a.clone b.object_id == a.object_id # => false a[1][2] = 'X' a[1] #66X722 b[1] #66X722 The copy should be different than the object. Why does it act like if it were just a reference? 回答1: You need to do a deep copy of your array. Here is the way to do it Marshal.load(Marshal.dump(a)) This is because you are