theweipeng commented on code in PR #1797:
URL: https://github.com/apache/fury/pull/1797#discussion_r1716240819


##########
rust/tests/tests/test_custom_trait.rs:
##########
@@ -0,0 +1,81 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use fury_core::error::Error;
+use fury_core::fury::Fury;
+use fury_derive::{impl_polymorph, Fury};
+use std::any::{Any, TypeId};
+
+#[impl_polymorph]
+trait Animal {
+    fn get_name(&self) -> String;
+}
+
+#[derive(Fury, Debug)]
+#[polymorphic_traits("Animal")]

Review Comment:
   > I think of one solution to this issue.
   > 
   > Before this PR, `dyn Any` is already supported, so solution below should 
work? Let me know if there is anything I'm missing.
   > 
   > > Suppose the trait already contains a `fn as_any(&self) -> &dyn Any` 
method, then we can use `trait_object.as_any().type_id()` to get underlying 
struct's type id, then we don't need this `polymorphic_traits`.
   > 
   > `as_any` solution is a frequently used tricks in rust, so I think adding 
this requirement is an acceptable choice.
   
   We already supported `dyn Any` , but even if we have the `type id ` of  the 
underlying struct,   I don't quite understand how to cast to the `dyn object` 
through type_id.
   
   My understanding of the solution is as follows
   
   ```Rust
   
   impl Serializer for Box<dyn Foo> {
       deserialize() {
             let type_id_in_binary = fury.reader.i16();  // We read the type_id 
from binary, which was received from another app.
             let concrete_type_deserializer = 
fury.get_deserializer(type_id_in_binary); // We obtain the deserializer that 
was registered by the user.
             let concrete_instance: Box<dyn Any> = 
concrete_type_deserializer();     // Call the function pointer, and we get the 
instance of the underlying struct. The type of the result should be Any, as the 
function pointer should be compatible with all the deserializers.
            
           concrete_instance as Box<dyn Foo> // We can't do this, as Rust can 
only cast from primitive types or concrete types. The compiler can't deduce the 
type of `concrete_instance` at compile time, so it can't create the v-table. 
       }
   }
   
   ```
    Let me know if there are some misunderstandings



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to