Vec<T>
only has 'one level' of indirection in the sense of:
We have the Vec object.
It points to somewhere no the heap, we do one heap access.
Rc<Vec<T>>
, in my current understanding, has 'two levels' of indirection in the sense of:
We have a Rc object.
We do one heap access to get the Vec<T>
object.
We then do another heap access to read the actual element.
Question: is there some object that behaves like a Rc<Vec<T>>
, but, when reading elements, only has 'one level' of indirection ?
2e71828
October 23, 2020, 10:56am
2
If you don’t need to grow/shrink the contents, you can use an Rc<[T]>
. This seems likely, since you weren’t asking about Rc<RefCell<Vec<T>>>
4 Likes
Phlopsi
October 23, 2020, 10:57am
3
You do know, that you cannot mutate the Vec through Rc? You may be able to mutate the elements of the vector with interior mutability, but you're unable to safely change the length or capacity of the vector or swap it.
2e71828
October 23, 2020, 11:04am
4
Well, there’s Rc::try_unwrap()
and Rc::make_mut()
which enable it under limited circumstances.
Phlopsi
October 23, 2020, 11:07am
5
For running code pre- and post-shared ownership, indeed. Not while sharing ownership.
Sorry for not clarifying in original post. In this particular case, the only ops I need are:
Vec<T> -> Weird_Rc_Vec<T>
and
Weird_Rc_Vec<T> :: get_item(i: usize)
i.e. after creation, the only op is reading.
Yandros
October 23, 2020, 11:29am
7
(Technically you may be using Rc<Vec<T>>
not to necessarily share , but rather to lazily clone / get CoW semantics (Rc::make_mut
), which may thus happen while "sharing").
anon80458984:
Vec<T> -> Weird_Rc_Vec<T>
and
Weird_Rc_Vec<T> :: get_item(i: usize)
i.e. after creation, the only op is reading.
type WeirdRcVec<T> = Rc<[T]>
,
using <Rc<[T]> as From<Vec<T>>>::from()
to construct it.
1 Like
system
Closed
January 21, 2021, 11:29am
8
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.