Questions tagged [ownership-semantics]

Ownership semantics are a set of rules that govern the lifetime of allocated resources. Ownership semantics determine when and how allocated resources can be freed, and whether ownership can be shared.

67 questions
4
votes
1 answer

weak self be used in DispatchQueue

Here is a simple API class as shown below. class API { func fetchData(_ callback: @escaping (Data) -> Void) { // reqeust to server... callback(response) } } When we call fetchData in the code below, we use [weak self] to…
3
votes
2 answers

Why does this "cannot move out of `self.x` which is behind a mutable reference" error happen?

I'm trying to write Tetris in rust. I have structs in this project that I want to treat as immutable even though they do mutate. The approach I'm using to achieve this kind of behavior is this: #[derive(Debug)] struct Example { foo: i8 } impl…
3
votes
2 answers

Is using std::span into an std::vector after push_back is called undefined behavior

My understanding of std::span is that it is essentially contains pointer into a container, a size, and some useful member functions. template class SimpleSpan { T* ptr; size_t length; // some member functions } I can take a…
3
votes
1 answer

Mutating a struct's fields after a move

I was puzzled by the following behavior: could someone explain what is going on? Consider the code: struct Point { cx : u32, } fn main() { let mut p1 = Point { cx: 100 }; let p2 = p1; p1.cx = 5000; // println!("p1.x =…
Ranjit Jhala
  • 1,242
  • 8
  • 18
3
votes
2 answers

Initialization and management of a shared_ptr to base interface

I have some questions related to the use of a shared_ptr pointing to a base class. Their answers influence each other, and for all three I need the same code snippet to set the context in as minimal a way as possible, like this (all the questions…
aPonza
  • 492
  • 4
  • 10
3
votes
1 answer

Cannot move out of borrowed content on enum containing a boxed trait object when deriving PartialEq

I'm trying to write an enum deriving PartialEq which contains a trait object which does so manually. I used the solution here in order to force implementors of Trait to write an equality method. This fails to compile: trait Trait { fn…
Zack
  • 43
  • 1
  • 2
3
votes
1 answer

Conditionally move T out from Rc when the count is 1

Is there a way to move an object from an Rc when the count is 1? I am thinking about how one would implement: fn take_ownership(shared: Rc) -> Result> { ... } The semantics would be that you get T if the count is 1 and you get…
Victor Savu
  • 936
  • 14
  • 19
3
votes
1 answer

Is there a way to avoid cloning when converting a PathBuf to a String?

I need to simply (and dangerously - error handling omitted for brevity) get the current executable name. I made it work, but my function converts a &str to String only to call as_str() on it later for pattern matching. fn binary_name() -> String { …
Maciej Goszczycki
  • 1,118
  • 10
  • 25
3
votes
1 answer

Why this function return an (owned) value?

the code from: Genie howto repeat a string N times as an string arrayGenie howto repeat a string N times as an string array def repeatwithsep (e: string, n: int, separator: string): string var elen = e.length; var slen = separator.length; …
2
votes
1 answer

vector of string slices goes out of scope but original string remains, why is checker saying there is an error?

Beginner at rust here. I understand why the code below has an error. test(x) creates y then returns a value that references the &str owned by y. y is destroyed as it goes out of scope so it can't do that. Here's my issue the thing is the &str owned…
Brian Yeh
  • 3,119
  • 3
  • 26
  • 40
2
votes
2 answers

Confuse about borrowing in Rust

I have 2 example code. 1st Example Code fn main() { let mut s = String::from("hello"); let r1 = &mut s; let r2 = & s; println!("{}", r2); } 2nd Example Code fn main() { let mut s = String::from("hello"); let r1 = &mut…
Hello World
  • 740
  • 5
  • 18
2
votes
0 answers

How can I release ownership of a resource managed by a Cleaner?

Suppose I have a wrapper object like this: private val CLEANER = java.lang.ref.Cleaner.create() internal typealias Ptr = Long private external fun acquireResource(): Ptr private external fun disposeResource(nativePtr: Ptr) /* ... */ class…
2
votes
2 answers

Ownership of global resources after fork

Consider a Foo that holds some resource struct Foo { ~Foo(); }; and a global std::vector. Perhaps stupid example, but it illustrates the problem well. std::vector bar; Now, the processes forks. If bar is then only modified by…
user877329
  • 6,717
  • 8
  • 46
  • 88
2
votes
1 answer

What happens when a copy of a shared pointer is created whose object contains a unique pointer?

I have been messing about with SFML, figuring out how a simple 2D game could be built. I just noticed this behaviour and couldn't figure out what's going on. Sample code for what is confusing me: struct Unique {}; class Shared { public: …
karnage
  • 183
  • 9
2
votes
1 answer

Getting around Rust ownership problems when using state machine pattern

This question is about a specific pattern of ownership that may arise when implementing a state machine for a video game in Rust, where states can hold a reference to "global" borrowed context and where state machines own their states. I've tried to…
Guy
  • 23
  • 3