Json Serialization feature of chrono crate - serialization

I'm trying to use DateTime from rust-chrono crate to my own trait.
#[derive(Debug, RustcEncodable, RustcDecodable)]
pub struct Accomplishment {
name: String,
accomplishment_type: String,
date: DateTime<UTC>
}
When I try to compile this it complains that
src/lib.rs:11:33: 11:47 error: the trait `rustc_serialize::serialize::Decodable` is not implemented for the type `chrono::datetime::DateTime<chrono::offset::utc::UTC>` [E0277]
src/lib.rs:11 #[derive(Debug, RustcEncodable, RustcDecodable)]
When I checked the github repo of chrono it had the rustc_serialize support implemented. But it is as a feature. In commit log it has
cargo test -v --features rustc-serialize
I'm not sure how to have this feature for my project. Can someone help me on how to use chrono with rustc-serialize?
There is a similar question regarding this. But what I wanted is to use the serialization support available in chrono in my project without implementing a wrapper trait.

Add the feature to your dependency in the Cargo.toml
[dependencies.chrono]
version = "*"
features = ["rustc-serialize"]
The relevant Documentation can be found here

Related

How to substitute dependency for a specific variant of Kotlin MPP dependency via attribute selectors?

This question has nothing to do with Kotlin multiplatform (Kotlin MPP) in that sense that from gradle point of view, Kotlin MPP configuration is just a particular case of multivariant build.
So, imagine I want to redefine particular variant of dependency, for instance, I want to have a javascript module redefined. This approach works perfectly well:
substitute(module("org.shabunc.demo:lib-js")).using(project(":lib-js"))
However it works because I know that for a common dependency "org.shabunc.demo" the "org.shabunc.demo-js" corresponds to the js variant of dependency.
I've decided to experiment with more advanced approach where I'm redefining the variant dependency, and I've tried to do something like this:
val kotlinPlatformTypeAttr = Attribute.of("org.jetbrains.kotlin.platform.type", KotlinPlatformType::class.java)
...
substitute(variant(module("org.shabunc.demo:lib")) {
attributes {
attribute(Usage.USAGE_ATTRIBUTE, objects.named(Usage::class.java, "kotlin-api"))
attribute(kotlinPlatformTypeAttr, KotlinPlatformType.js)
}
}).using(variant(project(":lib-js")) {
attributes {
attribute(Usage.USAGE_ATTRIBUTE, objects.named(Usage::class.java, "kotlin-api"))
attribute(kotlinPlatformTypeAttr, KotlinPlatformType.js)
}
})
However this not working as expected, the compileKotlinMetadata is not resolving any entity defined in a common part of org.shabunc.demo:lib
My question is pretty straightforward: How can I redefine specific variant of Kotlin MPP dependency without relying on its name rather than on the name of common dependency?

Why cannot be `const val` used in build.gradle.kts

I'd like to define a version constant in guild.gradle.kts file so that it can be used even in the plugins block. The plugins block requires restricted syntax:
«plugin version» must be constant, literal, strings
Following the restriction I tried to define the a version constant:
const val kotlinVersion = "1.3.72"
plugins {
java
kotlin("jvm") version kotlinVersion
}
However this fails with message
Const 'val' are only allowed on top level or in objects
even though the declaration seem to meet all const requirements. Why cannot be const val used in build.gradle.kts?
Even though it seems like your build script is top level, it isn't. The gradle docs mentions this when explaining the lifecycle of the build:
Finally, evaluate each Project by executing its build.gradle file, if present, against the project.
(source) This means that in your kotlin build scripts, the receiver type (i.e. this) is KotlinBuildScript which is eventually a subclass of Project. I don't know the details about how it's evaluated, but I can imagine it would be equivalent to what you can do in Kotlin with receiver types:
fun Project.evaluate(buildScript: Project.() -> Unit) = this.evaluate()
So your build script is really just the inside of a closure, hence why you can't use const val.

Jinq in Kotlin - how to convert lambda into java SerializedLambda?

Can I have serializable lambda in Kotlin? I am trying to use Jinq library from Kotlin, but it requires serializable lambdas. Is there any syntax that makes it possible?
Update:
My code:
var temp=anyDao.streamAll(Task::class.java)
.where<Exception,Task> { t->t.taskStatus== TaskStatus.accepted }
.collect(Collectors.toList<Task>());
I am getting this error:
Caused by: java.lang.IllegalArgumentException:
Could not extract code from lambda.
This error sometimes occurs because your lambda references objects that aren't Serializable.
All objects referenced in lambda are serializable (code results in no errors in java).
Update 2
After debugging it seems that kotlin lambda isn't translated into java.lang.invoke.SerializedLambda which is required by Jinq to get information from. So the problem is how to convert it to SerializedLambda.
I'm the maker of Jinq. I haven't had the time to look at Kotlin-support, but based on your description, I'm assuming that Kotlin compiles its lambdas into actual classes or something else. As such, Jinq would probably need some special code for cracking open Kotlin lambdas, and it may also need special code for handling any unusual Kotlin-isms in the generated code. Jinq should be capable of handling it because it was previously retrofitted to handle Scala lambdas.
If you file an issue in the Jinq github about it, along with a small Kotlin example (in both source and .class file form), then I can take a quick peek at what might be involved. If it's small, I can make those changes. Unfortunately, if it looks like a lot of work, I don't think I can really justify putting a lot of resources into adding Kotlin support to Jinq.
I have no experience on Jinq, but according to the implementation in GitHub and my experience of using Java Library in Kotlin.
ref: https://github.com/my2iu/Jinq/blob/master/api/src/org/jinq/orm/stream/JinqStream.java
You can always fall back to use the native Java Interface in Kotlin.
var temp = anyDao.streamAll(Task::class.java)
.where( JinqStream.Where<Task,Exception> { t -> t.taskStatus == TaskStatus.accepted } )
.collect(Collectors.toList<Task>());
// Alternatively, You you can import the interface first
import org.jinq.orm.stream.JinqStream.*
...
// then you can use Where instead of JinqStream.Where
var temp = anyDao.streamAll(Task::class.java)
.where(Where<Task,Exception> { t -> t.taskStatus == TaskStatus.accepted } )
.collect(Collectors.toList<Task>());
Or make a custom extension to wrap the implementation
fun JinqStream<T>.where(f: (T) -> Boolean): JinqStream<T> {
return this.where(JinqStream.Where<T,Exception> { f(it) })
}
Disclaimer: The above codes have not been tested.

What compiler option/library do I need to use detect_or_t type trait?

I am trying to use std::experimental::detect_or_t from <experimental/type_traits>.
What compiler, option, version or library do I need to compile the following example from http://en.cppreference.com/w/cpp/experimental/is_detected ?
#include <experimental/type_traits>
#include <cstddef>
template<class T>
using diff_t = typename T::difference_type;
template <class Ptr>
using difference_type = std::experimental::detected_or_t<std::ptrdiff_t, diff_t, Ptr>;
struct Meow { using difference_type = int; };
struct Purr {};
int main()
{
static_assert(std::is_same<difference_type<Meow>, int>::value, "Meow's difference_type should be int!");
static_assert(std::is_same<difference_type<Purr>, std::ptrdiff_t>::value, "Purr's difference_type should be ptrdiff_t!");
}
I tried using clang++ -std=c++14 and g++ -std=c++14. Also with -std=c++1y and -std=c++17. I always get this:
main.cpp:8:44: error: 'detected_or_t' in namespace 'std::experimental' does not name a template type
Those traits were first added to libstdc++ in GCC 6.1.0, as documented in the GCC 6 release notes:
Experimental support for most features of the second version of the Library Fundamentals TS.
And the implementation status tables in the manual, at
https://gcc.gnu.org/onlinedocs/gcc-6.1.0/libstdc++/manual/manual/status.html#table.cxx1z_ts_status
I'm less sure about libc++, but they're not supported by the version in Clang 3.9.1 but are supported by the current trunk, so I think they first appeared in Clang 4.0.0

How am I messing up these modules?

I'm trying to create a crate that has a library and one or more binaries. I've looked at Rust package with both a library and a binary? and the Rust book section on crates and modules but am still running into errors when I try and compile.
I've included the relevant sections of each file (I think).
../cargo.toml:
[package]
name = "plotmote"
version = "0.1.0"
authors = ["Camden Narzt <my#nice.email>"]
[lib]
name = "lib_plotMote"
path = "src/lib.rs"
[[bin]]
name = "plotMote"
path = "src/main.rs"
lib.rs:
pub mod lib_plotMote;
lib_plotMote/mod.rs:
pub mod LogstreamProcessor;
lib_plotMote/LogstreamProcessor.rs:
pub struct LogstreamProcessor {
main.rs:
extern crate lib_plotMote;
use lib_plotMote::LogStreamProcessor;
error:
cargo build
Compiling plotmote v0.1.0 (file:///Users/camdennarzt/Developer/Rust/plotmote)
main.rs:6:5: 6:37 error: unresolved import `lib_plotMote::LogStreamProcessor`. There is no `LogStreamProcessor` in `lib_plotMote` [E0432]
This should work:
use lib_plotMote::lib_plotMote::LogStreamProcessor;
The first lib_plotMote comes from extern crate, and the second one comes from the module you have defined in the library crate:
pub mod lib_plotMote;
Therefore, the library crate contains one module which, coincidentally, has the same name as the crate itself.
Also, as #starblue has noticed, you have case mismatch in the declaration site of the structure (LogstreamProcessor) and its use site (LogStreamProcessor). This should also be fixed.
As as side note, I suggest you to follow the idiomatic naming convention and avoid camelCase in module/crate names.