object-model

Is it legal to cast a struct to an array?

给你一囗甜甜゛ 提交于 2020-12-06 04:15:32
问题 Consider the following: // Just a sequence of adjacent fields of same the type #[repr(C)] #[derive(Debug)] struct S<T> { a : T, b : T, c : T, d : T, } impl<T : Sized> S<T> { fn new(a : T, b : T, c : T, d : T) -> Self { Self { a, b, c, d, } } // reinterpret it as an array fn as_slice(&self) -> &[T] { unsafe { std::slice::from_raw_parts(self as *const Self as *const T, 4) } } } fn main() { let s = S::new(1, 2, 3, 4); let a = s.as_slice(); println!("s :: {:?}\n\ a :: {:?}", s, a); } Is this code

Is it legal to cast a struct to an array?

喜欢而已 提交于 2020-12-06 04:14:20
问题 Consider the following: // Just a sequence of adjacent fields of same the type #[repr(C)] #[derive(Debug)] struct S<T> { a : T, b : T, c : T, d : T, } impl<T : Sized> S<T> { fn new(a : T, b : T, c : T, d : T) -> Self { Self { a, b, c, d, } } // reinterpret it as an array fn as_slice(&self) -> &[T] { unsafe { std::slice::from_raw_parts(self as *const Self as *const T, 4) } } } fn main() { let s = S::new(1, 2, 3, 4); let a = s.as_slice(); println!("s :: {:?}\n\ a :: {:?}", s, a); } Is this code

Why is there a rule that temporary objects must have distinct addresses?

落花浮王杯 提交于 2019-12-23 12:09:45
问题 The situation of my interest is const int &n1 = 123; const int &n2 = 123; I know it is something as if the literal 123 is the parameter for initializing a temporary int and const is just a boring compile time check, but I want to know the reason why distinct temporaries are needed in this case, instead of n1 and n2 both having the same temporary. I know the rule exists but do not know why this rule exists. 回答1: If you want them to have the same address, you can always do const int &n2 = n1 .

In Squeak, how to wrap every method send?

女生的网名这么多〃 提交于 2019-12-12 20:20:47
问题 I created a class, and in that class I have a method 'sendMessage: to: withArgs:' which recieves an object, a message and an array of arguments. The method is used to send messages to object and perform some algorithm. To use this method I have to create an instance x of the class I created and do something like x sendMessage: '+' to: '7' withArgs: '#(5)'. The result of this sending the message '+' to the object 7 with the parameter 5, plus some stuff that my algorithm does. But what I want

Exposing common values from a custom structure/type

不羁岁月 提交于 2019-12-12 11:42:03
问题 One of my projects has a value type/struct that represents a custom identifier string for a video format. In this case, it's going to contain a content type string, but that can vary. I've used a struct so it can be strongly type when it's passed around, and perform some sanity checks on the initial string value. public struct VideoFormat { private string contentType; public VideoFormat(string contentType) { this.contentType = contentType; } public string ContentType { get { return this

How do I overcome XCode object model version error?

六眼飞鱼酱① 提交于 2019-12-11 04:47:16
问题 I'm currently running OS X 10.7.4 and using XCode 4.3.2. I'm coming back to XCode from not really using it in production since version 2.x. That said, I have a test Core Data application (MacOS not iPhone) I'm getting familiar with. There is 0 code -- all Interface Builder at the moment. I changed the data model a couple days ago, and have been getting the following error since: The managed object model version used to open the persistent store is incompatible with the one that was used to

Custom structure/type that can be used with switch()

只谈情不闲聊 提交于 2019-12-10 03:36:39
问题 One of my projects has a value type/struct that represents a custom identifier string for a video format. In this case, it's going to contain a content type string, but that can vary. I've used a struct so it can be strongly type when it's passed around, and perform some sanity checks on the initial string value. The actual string value could be anything and provided by external plugins libraries so a numeric enum doesn't apply. public struct VideoFormat { private string contentType; public

When should transient properties in CoreData be included in the object model?

我的梦境 提交于 2019-12-08 16:23:16
问题 I am unsure about the correct definition of transient properties: One can define transient properties in the object model and then calculate them when needed in the related class. But things work just as well if you specify a class and define arbitrary getter methods for any transient property without declaring it in the object model (as long as the entity is linked to that class in the model). My question: What is the benefit of declaring transient properties in the object model? I only see

At what point does the lifetime of a trivial type created by placement-new start?

旧巷老猫 提交于 2019-12-06 01:08:17
问题 During a dive into dynamic memory, it occurred to me it appears contradictory how trivial types begin their lifetime. Consider the snippet void* p = ::operator new(sizeof(int)); // 1 // 2 new (p) int; // 3 When does the int start its lifetime? Only acquires storage, ::operator new is specified to have the effect (from [new.delete.single]) The allocation functions called by a new-expression to allocate size bytes of storage. [...] allocates storage suitably aligned to represent any object of

Custom structure/type that can be used with switch()

南楼画角 提交于 2019-12-05 03:39:18
One of my projects has a value type/struct that represents a custom identifier string for a video format. In this case, it's going to contain a content type string, but that can vary. I've used a struct so it can be strongly type when it's passed around, and perform some sanity checks on the initial string value. The actual string value could be anything and provided by external plugins libraries so a numeric enum doesn't apply. public struct VideoFormat { private string contentType; public VideoFormat(string contentType) { this.contentType = contentType; } public string ContentType { get {