-
Notifications
You must be signed in to change notification settings - Fork 91
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[MoveosStd] Refactor ObjectRef, context:new_object and context::remove_object #948
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎ 1 Ignored Deployment
|
new_object_with_id(self, object_id, owner, value) | ||
} | ||
|
||
public(friend) fun new_object_with_id<T: key>(self: &mut Context, id: ObjectID, owner: address, value: T) : ObjectRef<T> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If new an object and return ObjectRef
by default, need to additionally call the object_ref::id
method to get the object_id. Looking at the example, only need the object_id in many cases, which will increase the burden to the developer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The return value here is actually to save the Ref. In this way, we can save the creator's permissions, so that anyone who needs to can operate the object anywhere. If the return value is ObjectID, we can only add a field in to determine whether the user has permission to operate the object.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Previously, developers needed to call object::id(&obj)
to get ObjectID, now, replaced by object_ref::id(&obj_ref)
.
ObjectRef is a way to divide Object permissions. ObjectRef allows anyone to operate on the Object body. Should we abstract the permissions of sending and extending Objects, so that Objects are more flexible? |
Do you have some ideas about this? |
Here, we limit the sending of Objects of type to the module of T. We can modify Objects so that they can be sent anywhere. We can limit the sending permissions of Objects by adding some fields. Only users who have TransferRef can send this Object.
As a result, all Objects can be sent arbitrarily, and the creator can close the sending of some Objects. Here is the implementation of Aptos, which I think is very good. |
We can provide a |
I believe it is necessary, As a special type, Object should have some differences from ordinary structs, such as inheritance, polymorphism, and extensibility. |
aa85fd7
to
34b56aa
Compare
34b56aa
to
e79f988
Compare
public fun add_object<T: key>(self: &mut Context, obj: Object<T>) { | ||
storage_context::add<T>(&mut self.storage_context, obj) | ||
/// Remove object from object store, and unpack the Object | ||
public fun remove_object<T: key>(self: &mut Context, object_id: ObjectID): (ObjectID, address, T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After remove object, the original ObjectRef reference still exists. If call object_ref::borrrow
or object_ref::borrrow_mut
, the underlying raw_table will throw a NotFound error code. The Rust life cycle management mechanism fails in this dimension.
Should we consider adding track ObjectRef?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This problem does exist, So is it better to only handle the Object life cycle through Ref?
Only read using ID
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Record a TODO about this
rooch/moveos/moveos-stdlib/moveos-stdlib/sources/object_ref.move
Lines 22 to 25 in e79f988
public fun new<T: key>(object: &mut Object<T>) : ObjectRef<T> { | |
//TODO should we track the reference count? | |
new_internal(object) | |
} |
Currently, before using ObjectRef, the dev needs to call object_ref::exist_object
to ensure the object exists.
Summary
[MoveosStd] Refactor ObjectRef, context:new_object and context::remove_object
ObjectRef
can notcopy
.context::new_object<T>
auto add Object to storage, and return ObjectRefcontext::remove_object<T>
auto unpack the Object, and return(id, owner, T)
Continue #935
Part of #901
@geometryolife Some documents and example readme need to be updated.
@wubuku the code generator needs to be updated with new Object and ObjectRef API.