traits

Why can't I cast a Box with an extended trait to a Box with the base trait? [duplicate]

北战南征 提交于 2021-02-10 17:08:14
问题 This question already has answers here : Why doesn't Rust support trait object upcasting? (3 answers) Closed 2 years ago . Given the code trait Base { } trait Derived : Base { } struct Foo { } impl Base for Foo { } impl Derived for Foo { } fn main() { let b : Box<Derived> = Box::new( Foo { } ); let a : Box<Base> = b; } When I compile as I'm sure you know I get the following error message: error[E0308]: mismatched types --> src/main.rs:14:25 | 14 | let a : Box<Base> = b; | ^ expected trait

Why can't I cast a Box with an extended trait to a Box with the base trait? [duplicate]

偶尔善良 提交于 2021-02-10 17:05:19
问题 This question already has answers here : Why doesn't Rust support trait object upcasting? (3 answers) Closed 2 years ago . Given the code trait Base { } trait Derived : Base { } struct Foo { } impl Base for Foo { } impl Derived for Foo { } fn main() { let b : Box<Derived> = Box::new( Foo { } ); let a : Box<Base> = b; } When I compile as I'm sure you know I get the following error message: error[E0308]: mismatched types --> src/main.rs:14:25 | 14 | let a : Box<Base> = b; | ^ expected trait

Why can't I cast a Box with an extended trait to a Box with the base trait? [duplicate]

喜你入骨 提交于 2021-02-10 17:04:35
问题 This question already has answers here : Why doesn't Rust support trait object upcasting? (3 answers) Closed 2 years ago . Given the code trait Base { } trait Derived : Base { } struct Foo { } impl Base for Foo { } impl Derived for Foo { } fn main() { let b : Box<Derived> = Box::new( Foo { } ); let a : Box<Base> = b; } When I compile as I'm sure you know I get the following error message: error[E0308]: mismatched types --> src/main.rs:14:25 | 14 | let a : Box<Base> = b; | ^ expected trait

Why is implementing an external trait using my type as a parameter for an external type legal?

若如初见. 提交于 2021-02-09 11:15:15
问题 I am modifying some code to depend on rand version 0.5. At first, I was worried how I would enable generating random values of my own types with Standard , but I found out this is legal: impl ::rand::distributions::Distribution<MyType> for ::rand::distributions::Standard { // ... } Why is it legal? I thought implementing an external trait for an external type is illegal. 回答1: The entire purpose of these rules (called the orphan rules or coherence rules ) is to avoid having any conflicting

What kind of “Traits” are used/defined in the C++0x Standard

拜拜、爱过 提交于 2021-02-05 20:04:43
问题 A trait in C++ encapsulates a family of operations that allow an Algorithm or Data Structure to operator with that type with which it is instantiated. char_traits are an example for grouping string - and file-required functions. But not all traits have "trait" in their name, right? numeric_limits comes to mind. Is this a "Trait", too? Even without the name "trait" in it? So, are there other Templates that could/should be considered a "Trait"? Besides the examples I found: allocator_traits how

What kind of “Traits” are used/defined in the C++0x Standard

拟墨画扇 提交于 2021-02-05 20:01:12
问题 A trait in C++ encapsulates a family of operations that allow an Algorithm or Data Structure to operator with that type with which it is instantiated. char_traits are an example for grouping string - and file-required functions. But not all traits have "trait" in their name, right? numeric_limits comes to mind. Is this a "Trait", too? Even without the name "trait" in it? So, are there other Templates that could/should be considered a "Trait"? Besides the examples I found: allocator_traits how

What kind of “Traits” are used/defined in the C++0x Standard

◇◆丶佛笑我妖孽 提交于 2021-02-05 20:00:37
问题 A trait in C++ encapsulates a family of operations that allow an Algorithm or Data Structure to operator with that type with which it is instantiated. char_traits are an example for grouping string - and file-required functions. But not all traits have "trait" in their name, right? numeric_limits comes to mind. Is this a "Trait", too? Even without the name "trait" in it? So, are there other Templates that could/should be considered a "Trait"? Besides the examples I found: allocator_traits how

C++ How to specialize a template using vector<T>?

北战南征 提交于 2021-02-05 10:27:19
问题 Basicly ,I want to make a function behave differently for a vector(type) parameter and a non-vector type parameter . #include <vector> using namespace std; template <typename type> struct is_vector { static const bool value = false; }; template <typename type> struct is_vector<vector<type>> { static const bool value = true; }; template <typename type> type read() { if (is_vector<type>::value) { type vec(10); vec.front()=1;//left of '.front' must have class/struct/union return vec; } else {

C++ How to specialize a template using vector<T>?

孤街醉人 提交于 2021-02-05 10:27:00
问题 Basicly ,I want to make a function behave differently for a vector(type) parameter and a non-vector type parameter . #include <vector> using namespace std; template <typename type> struct is_vector { static const bool value = false; }; template <typename type> struct is_vector<vector<type>> { static const bool value = true; }; template <typename type> type read() { if (is_vector<type>::value) { type vec(10); vec.front()=1;//left of '.front' must have class/struct/union return vec; } else {

Laravel encrypt cannot encrypt to database when using Update method

谁说胖子不能爱 提交于 2021-02-05 07:52:09
问题 Hi i want to encrypt some field in database when user create or edit data. IF create, the encryption work, but when user edit data, the value that save in database will be normal text not encrypted <?php namespace App\Traits; use Crypt; trait Encryptable { public function toArray() { $array = parent::toArray(); foreach ($array as $key => $attribute) { if (in_array($key, $this->encryptable) && $array[$key]!='') { try { $array[$key] = Crypt::decrypt($array[$key]); } catch (\Exception $e) { } }