[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
Re: [tor-bugs] #23881 [Core Tor/Tor]: Implement a way to utilise tor's logging system from Rust code
#23881: Implement a way to utilise tor's logging system from Rust code
-----------------------------------------------+---------------------------
Reporter: isis | Owner: isis
Type: enhancement | Status: accepted
Priority: High | Milestone: Tor:
| 0.3.3.x-final
Component: Core Tor/Tor | Version:
Severity: Normal | Resolution:
Keywords: rust, rust-pilot, review-group-29 | Actual Points:
Parent ID: | Points: 3
Reviewer: nickm | Sponsor:
-----------------------------------------------+---------------------------
Comment (by isis):
Replying to [comment:37 manish.earth]:
> Yeah, I'd missed the "not" in the second half.
>
>
> You're right that `CString::_new` does no copies or allocations; the
sneaky bit is the `t.into()` -- the `From<&str>` impl for `Vec<u8>`. That
itself calls the `From<&[u8]> for Vec<u8>` impl (https://github.com/rust-
lang/rust/blob/29f5c699b11a6a148f097f82eaa05202f8799bbc/src/liballoc/vec.rs#L2161-L2171)
:
>
> {{{#!rust
> impl<'a, T: Clone> From<&'a [T]> for Vec<T> {
> #[cfg(not(test))]
> fn from(s: &'a [T]) -> Vec<T> {
> s.to_vec()
> }
> #[cfg(test)]
> fn from(s: &'a [T]) -> Vec<T> {
> ::slice::to_vec(s)
> }
> }
> }}}
>
> which calls `alloc::slice::to_vec` (https://github.com/rust-
lang/rust/blob/29f5c699b11a6a148f097f82eaa05202f8799bbc/src/liballoc/slice.rs#L164-L170),
which creates a new vector and copies stuff into it via
`extend_from_slice`
>
> {{{#!rust
> pub fn to_vec<T>(s: &[T]) -> Vec<T>
> where T: Clone
> {
> let mut vector = Vec::with_capacity(s.len());
> vector.extend_from_slice(s);
> vector
> }
> }}}
>
> If `T` is a `String` or `Vec<u8>`, it won't be copied, because the
`From<String> for Vec<u8>` impl is basically a no-op move. But if it's a
borrow (e.g. `&str` or `&[u8]` or something), it will be copied into a new
allocation. It has to be -- you can't guarantee that borrow will live
forever.
Aha, I missed that part, thanks! So it'll be copied even if the originally
borrowed thing is `&'static`?
--
Ticket URL: <https://trac.torproject.org/projects/tor/ticket/23881#comment:38>
Tor Bug Tracker & Wiki <https://trac.torproject.org/>
The Tor Project: anonymity online
_______________________________________________
tor-bugs mailing list
tor-bugs@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-bugs