Deref Trait का उपयोग
Deref एक wrapper type को सिखाता है कि इसके रखे value की तरह कैसे act करना है, ताकि आप इसे लगभग ऐसे उपयोग कर सकें जैसे यह wrapped ही न हो।In this page:
impl Deref for Name {
type Target = Inner;
fn deref(&self) -> &Self::Target {
&self.field
}
}
Box Already Implements Deref
Box<T> Deref implement करता है, यही कारण है कि *boxed_value आपको सीधे inner value देता है, भले ही Box खुद एक distinct type है।
उदाहरण: Box Already Implements Deref
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `boxed`, set to `Box::new(5)`
let boxed = Box::new(5);
// Print "Dereferenced: {}" to the console, substituting in `*boxed` (with a trailing newline)
println!("Dereferenced: {}", *boxed);
}
Login to try C/C++/Java/PHP code in the editor
Implementing Deref for a Custom Wrapper
आप अपने खुद के wrapper struct के लिए Deref implement कर सकते हैं, यह define करते हुए कि *wrapper को क्या produce करना चाहिए और method calls के लिए deref coercion enable करते हुए।
उदाहरण: Implementing Deref for a Custom Wrapper
// Bring `std::ops::Deref` into scope
use std::ops::Deref;
// Define a struct `MyBox` to group related fields together
struct MyBox<T>(T);
// Implement the `Deref` trait for `MyBox`
impl<T> Deref for MyBox<T> {
type Target = T;
// Define the function `deref` taking `&self` and returning a `&T`
fn deref(&self) -> &T {
&self.0
}
}
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `wrapped`, set to `MyBox(String::from("wrapped value"))`
let wrapped = MyBox(String::from("wrapped value"));
// Print "{}" to the console, substituting in `*wrapped` (with a trailing newline)
println!("{}", *wrapped);
}
Login to try C/C++/Java/PHP code in the editor
Deref Coercion in Method Calls
किसी method को call करते समय, Rust अपने आप deref coercion apply करता है, &MyBox<String> को ज़रूरत के अनुसार &str में convert करते हुए, ताकि आप wrapper पर सीधे String/str methods call कर सकें।
उदाहरण: Deref Coercion in Method Calls
// Bring `std::ops::Deref` into scope
use std::ops::Deref;
// Define a struct `MyBox` to group related fields together
struct MyBox<T>(T);
// Implement the `Deref` trait for `MyBox`
impl<T> Deref for MyBox<T> {
type Target = T;
// Define the function `deref` taking `&self` and returning a `&T`
fn deref(&self) -> &T {
&self.0
}
}
// Define the function `print_len` taking `s`
fn print_len(s: &str) {
// Print "Length: {}" to the console, substituting in `s.len()` (with a trailing newline)
println!("Length: {}", s.len());
}
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `wrapped`, set to `MyBox(String::from("coerced"))`
let wrapped = MyBox(String::from("coerced"));
print_len(&wrapped);
}
Login to try C/C++/Java/PHP code in the editor
DerefMut for Mutable Access
Deref के साथ DerefMut implement करना wrapper type के ज़रिए mutable dereferencing (*wrapper = ... या &mut self methods call करना) भी allow करता है।
उदाहरण: DerefMut for Mutable Access
// Bring `std::ops::{Deref, DerefMut}` into scope
use std::ops::{Deref, DerefMut};
// Define a struct `MyBox` to group related fields together
struct MyBox<T>(T);
// Implement the `Deref` trait for `MyBox`
impl<T> Deref for MyBox<T> {
type Target = T;
// Define the function `deref` taking `&self` and returning a `&T { &self.0 }`
fn deref(&self) -> &T { &self.0 }
}
// Implement the `DerefMut` trait for `MyBox`
impl<T> DerefMut for MyBox<T> {
// Define the function `deref_mut` taking `self` and returning a `&mut T { &mut self.0 }`
fn deref_mut(&mut self) -> &mut T { &mut self.0 }
}
// Entry point: execution of the program starts here
fn main() {
// Declare a mutable variable `wrapped`, initialized to `MyBox(10)`
let mut wrapped = MyBox(10);
*wrapped += 5;
// Print "{}" to the console, substituting in `*wrapped` (with a trailing newline)
println!("{}", *wrapped);
}
Login to try C/C++/Java/PHP code in the editor
- यह भूल जाना कि
Derefसिर्फ*valueऔर method calls के behave करने के तरीके को प्रभावित करता है -- यह wrapper की खुद की type identity नहीं बदलता। - यह मान लेना कि
Derefimplement करना आपको अपने आपDerefMutभी देता है -- mutable dereferencing को एक अलग trait implementation चाहिए। - Inheritance-जैसे behavior fake करने के लिए custom
Derefimplementations overuse करना, जो code को follow करने में confusing बना सकता है।
Derefimplement करना एक custom type को*से dereferenced होने देता है,Box<T>के काम करने के तरीके जैसा।- Deref coercion methods call करते समय अपने आप
&MyTypeको&InnerTypeमें convert करता है, manual dereferencing घटाते हुए। DerefMutmutable dereferencing के लिए corresponding trait है और इसे अलग से implement किया जाना चाहिए।Derefवही है जोBox,Rc, और custom wrapper types जैसे smart pointers को लगभग उनके inner value की तरह उपयोग होने देता है।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: