英文:
Type-erase owner in self-referential crates
问题
我有一个结构体,并希望定义另一个引用它字段的结构体。
struct Tweet {
  text: Text
}
#[ouroboros::self_referencing]
struct BorrowedText<Owner> {
  owner: Arc<Owner>, // 确保在借用时不会被释放
  #[borrows(owner)]
  borrowed: &'this Text,
}
可以有许多类型,其中包含Text作为成员。为了防止BorrowedText<T>创建过多的类型,我想要擦除所有者类型。
#[ouroborus::self_referencing]
struct BorrowedText {
  owner: Arc<dyn Any>, // 确保在借用时不会被释放
  #[borrows(owner)]
  borrowed: &'this Text,
}
为此,我需要从owner: Arc<Tweet>获取&Text,然后将所有者上转换为Arc<dyn Any>。我无法使用ouroborus或owning_ref API(owning_ref甚至有专用的擦除类型,如ErasedBoxRef)来使这个工作。
是否有使用ErasedBoxRef或其他API来实现这一点的方法?或者我可以安全地执行下面的操作,而无需自引用的库?
BorrowedText { owner: owner.clone(), borrowed: unsafe { (&owner.text) as (*const Text) } }
英文:
I have a struct, and want to define another struct that references its field.
struct Tweet {
  text: Text
}
#[ouroboros::self_referencing]
struct BorrowedText<Owner> {
  owner: Arc<Owner>, // make sure it's not deallocated while borrowing
  #[borrows(owner)]
  borrowed: &'this Text,
}
There can be many types that have Text as a member. To prevent BorrowedText<T> from creating too many types, I would like to erase the owner type.
#[ouroborus::self_referencing]
struct BorrowedText {
  owner: Arc<dyn Any>, // make sure it's not deallocated while borrowing
  #[borrows(owner)]
  borrowed: &'this Text,
}
For this I need to get &Text from owner: Arc<Tweet> and then upcast owner to Arc<dyn Any>. I couldn't make this work with ouroborus or owning_ref API (owning_ref even has dedicated erased-types like ErasedBoxRef).
Is there any way to achieve this with ErasedBoxRef or other API? Or can I just safely do the below without the need for self-referential crates?
BorrowedText { owner: owner.clone(), borrowed: unsafe { (&owner.text) as (*const Text) } }
答案1
得分: 0
Manually unsafe-transmuting worked without a cost of dynamic downcast
fn f(owner: Arc<Tweet>) {
  let borrowed = unsafe { std::mem::transmute::<&'_ _, &'static _>(&owner.text) };
  BorrowedText {
    owner: owner as Arc<dyn Send + Sync>,
    borrowed,
  }
}
英文:
Manually unsafe-transmuting worked without a cost of dynamic downcast
fn f(owner: Arc<Tweet>) {
  let borrowed = unsafe { std::mem::transmute::<&'_ _, &'static _>(&owner.text) };
  BorrowedText {
    owner: owner as Arc<dyn Send + Sync>,
    borrowed,
  }
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论