These are chat archives for rust-lang/rust

7th
Dec 2017
Sherab Giovannini
@Shaddy
Dec 07 2017 13:50
morning
the usage would be just mutating the vectors w/o pushing or removing any item, so it can be mutating contiguous memory as independent mutate references of vec
Denis Lisov
@tanriol
Dec 07 2017 13:56
Why not use slices if you don't need to push or remove items?
To me, this feels... dirty. The normal use of unsafe in Rust is isolated from the consumer: "here is my module, the implementation uses unsafe inside, but you don't need to care, all the external interfaces are safe".
In your case, you return Vecs which look safe, but actually are not. There's a contract that memory safety depends upon - "just mutating the vectors w/o pushing or removing any item" - that the function's consumer needs to follow, but the compiler knows nothing about and cannot enforce.
Denis Lisov
@tanriol
Dec 07 2017 14:02
At the very least, such a function, in my opinion, must be unsafe itself.
By the way, please note that you seem to expose the memory without initializing it.
Denis Lisov
@tanriol
Dec 07 2017 14:21
@Shaddy What about the following safe and stable version?
Restioson
@Restioson
Dec 07 2017 14:42

@tanriol so remember a while ago I asked about a CoerceToSomeTrait trait and you suggested any? I have this https://play.rust-lang.org/?gist=78aa0cbbe3e658dcd511994e117b55f4&version=nightly but it only works for stuff where you have the concrete type -- I.e I can't do

let x: &TraitB = &MyStruct;
x.coerce().unwrap();

Any suggestions? I don't want to use Any preferably because it limits usages to 'static

Denis Lisov
@tanriol
Dec 07 2017 15:41
@Restioson I'm afraid that may be impossible for two independent traits. Basically, if you have let x: &TraitA = &MyStruct, it contains a pointer to the object and a pointer to the vtable of the impl TraitA for MyStruct. However, that vtable knows nothing about TraitB and does not contain a vtable pointer for the impl TraitB for MyStruct.
Denis Lisov
@tanriol
Dec 07 2017 15:50
The cause for the 'static limitation is very simple: the lifetimes information is not available at runtime at all. As a result, Rust can't check during downcasting whether the lifetime you suggest is valid or not.
Sherab Giovannini
@Shaddy
Dec 07 2017 16:13
@tanriol your suggestions and comments are best :)
thanks a lot
I didn't knew that I could have contiguous memory w/o being unsafy
*are the best
Sherab Giovannini
@Shaddy
Dec 07 2017 16:43
@tanriol, the problem with the cleaner version is that I can't give the ownership of those &mut [u8] to a thread
because they are references to the vector, and thats the whole thing about this
to be able to transfer ownership of contiguous memory to a couple of threads
is it possible?
Sean
@seanr707
Dec 07 2017 18:04
@shaddy I'm jumping in a little late, so forgive me if these have already been discussed, but would it work to use an Arc->Mutex to solve the issue? Or outright clone the slice?
Sherab Giovannini
@Shaddy
Dec 07 2017 18:19
the problem in my particular situation, is that I require to go in certain things out of Rust
I have a reference to a buffer, which is fixed size, and this is something a foreign preallocates for me, I will just get a reference, and split in also fixed sized parts then each thread will work with one chunk of the whole contiguous memory
I don't require to have native Rust syncronizations
having the threads fighting to acquire the reference is something I want to avoid
as you suggested, using Arc->Mutex leads into using CriticalSections (in the case of windows) to syncronize the access to this reference, which is something I don't want
it's not a problem for me to give access to the reference at same time, each thread owns his particular chunk
Sherab Giovannini
@Shaddy
Dec 07 2017 18:25
another option would be to just give a pointer to the thread and unsafely construct a &mut [u8; fixedsize]
Denis Lisov
@tanriol
Dec 07 2017 18:38
@Shaddy Have you looked into the bytes crate?
It seems to be the combination you need: reference-counted owning references into a shared buffer.
Sherab Giovannini
@Shaddy
Dec 07 2017 18:39
it feels really appropiate!
how do you get into that kind of suggestions? xD
Sean
@seanr707
Dec 07 2017 19:27

First Rust project pushed to Github

Alpha here for any linux user who want to try GitterGtk

Denis Lisov
@tanriol
Dec 07 2017 19:51
@Shaddy Reading a lot, writing some Rust for $DAYJOB, so now I usually know what to ask Google :-)